简体   繁体   English

UITapGestureRecognizer xamarin-多次轻按可快速显示多个视图

[英]UITapGestureRecognizer xamarin - tapping multiple times quickly displays multiple views

I'm having some difficulties solving a problem in xamarin. 解决xamarin中的问题时遇到一些困难。 In ViewDidAppear of my viewcontroller I am registering a tap gesture. 在我的viewcontroller的ViewDidAppear中,我正在注册一个轻击手势。

ProfileViewController 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);

        }

I then have my ShowBonus method which takes tap as a parameter. 然后,我有了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);

                    });

        }

Show bonus displays another view infront of the profile view. 显示奖金会在个人资料视图的前面显示另一个视图。 If I tap the UI quickly it will fire the code twice. 如果我快速点击UI,它将两次触发代码。 Is it possible that even if I tap it multiple times quickly that it only ever fires the code once? 即使我多次轻按它,它也可能只触发一次代码吗?

Define variable lastShowPress of type DateTime and store there last time of ShowBonus was called. 定义日期时间类型的变量lastShowPress并将其存储在上一次调用ShowBonus的时间。 At the begining of ShowBonus check if ShowBonus was called within let say 10 seconds or whatever interval you need. 在ShowBonus开始时,检查是否在10秒之内或您需要的任何间隔内调用ShowBonus。 If it was - return, if not - store the time and execute. 如果是,则返回-如果不是,则存储时间并执行。 You will need to protect this variable/checking by lock object. 您将需要保护此变量/通过锁定对象进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM