我在解决 xamarin 中的问题时遇到了一些困难。在我的 View Controller 的 ViewDidAppear 中,我正在注册一个点击手势。
ProfileViewController
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
if (!_hasUi) {
SetUi ();
}
SetProfile ();
if (CheckConnectivityStatus ()) {
SetGameData ();
if (_hasRegisteredGestures) return;
//SetGestures();
AssignGesturesToUi();
_hasRegisteredGestures = true;
} else {
HandleWebAPICallError ();
}
}
public void AssignGesturesToUi()
{
_bonusTap = new UITapGestureRecognizer(ShowBonus) { CancelsTouchesInView = false };
_userProfile.VwLevel.MultipleTouchEnabled = false;
_userProfile.VwLevel.UserInteractionEnabled = true;
_userProfile.VwLevel.AddGestureRecognizer(_bonusTap);
}
然后我的 ShowBonus 方法将 Tap 作为参数。
private void ShowBonus (UITapGestureRecognizer tap)
{
if (tap.State == UIGestureRecognizerState.Cancelled)
return;
_userProfile.VwLevel.UserInteractionEnabled = true;
//RemoveGestures();
var tab = TabBarController as GameTabController;
tab?.SetTabBarVisible(false, true);
var mainStoryboard = UIStoryboard.FromName("Main", null);
var achievementsViewController = (AchievementsViewController)mainStoryboard.InstantiateViewController("AchievementsViewController");
AddChildViewController(achievementsViewController);
achievementsViewController.Achievements = false;
achievementsViewController.View.Transform = CGAffineTransform.MakeScale(0.01f, 0.01f);
achievementsViewController.View.Center = _userProfile.ConvertPointToView(_userProfile.Cup.Center, View);
achievementsViewController.ReturnPoint = achievementsViewController.View.Center;
View.AddSubview(achievementsViewController.View);
achievementsViewController.View.Alpha = 0.0f;
UIView.Animate(0.5f, 0.0f, UIViewAnimationOptions.CurveEaseInOut,
() => {
achievementsViewController.View.Transform = CGAffineTransform.MakeScale(1.0f, 1.0f);
achievementsViewController.View.Center = View.Center;
achievementsViewController.View.Alpha = 1.0f;
},
() => {
achievementsViewController.DidMoveToParentViewController(this);
});
}
显示奖励在配置文件 View 前面显示另一个 View 。如果我快速点击 UI,它将触发代码两次。是否有可能即使我快速多次点击它,它也只会触发一次代码?
Best Answer-推荐答案 strong>
定义 DateTime 类型的变量 lastShowPress 并存储上次调用 ShowBonus 的时间。在 ShowBonus 开始时,检查是否在 10 秒或您需要的任何时间间隔内调用了 ShowBonus。如果是 - 返回,如果不是 - 存储时间并执行。您将需要保护此变量/通过锁定对象进行检查。
关于c# - UITapGestureRecognizer xamarin - 多次点击快速显示多个 View ,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/40607242/
|