简体   繁体   中英

UISwipeGestureRecognizer not working in iOS 4.3

This code worked fine in prior iOS versions but now in iOS 4.3 it's no longer working. Anyone have any suggestions on what needs to be corrected?

In the FinishedLaunching event I call AddSyncGesture and then whenever the device is rotated I have a notifier that calls the method as well as vertical changes with the orientation change so I have to remove and re-add it.

        UISwipeGestureRecognizer sgr = null;

    public void AddSyncGesture()
    {
        try {
            if (sgr != null)
                window.RemoveGestureRecognizer(sgr);
            else
                sgr = new UISwipeGestureRecognizer();
        } catch (Exception ex) {
            Logger.LogException(ex.Message, ex, "AddSyncGesture");
        }
        try {
            sgr.NumberOfTouchesRequired = 2;
            if (UIDevice.CurrentDevice.Orientation == UIDeviceOrientation.LandscapeRight || UIDevice.CurrentDevice.Orientation == UIDeviceOrientation.LandscapeLeft)
                sgr.Direction = UISwipeGestureRecognizerDirection.Left | UISwipeGestureRecognizerDirection.Right;
            else
                sgr.Direction = UISwipeGestureRecognizerDirection.Up | UISwipeGestureRecognizerDirection.Down;
            sgr.Delegate = new SwipeRecognizerDelegate();
            sgr.AddTarget(this, SwipeSelector); 
            window.AddGestureRecognizer(sgr);
        } catch (Exception ex) {
            Logger.LogException(ex.Message, ex, "AddSyncGesture");
        }
    }

    Selector SwipeSelector
    {
        get { return new Selector("HandleSwipe"); } 
    }

    [Export("HandleSwipe")]
    public void HandleSwipe(UISwipeGestureRecognizer recognizer)
    {
        if (Utils.CanSync)
            Application.Synchronizer.Synchronize(true,Application.ProgressView);    
    }

    class SwipeRecognizerDelegate : MonoTouch.UIKit.UIGestureRecognizerDelegate
    {
        public override bool ShouldReceiveTouch (UIGestureRecognizer recognizer, UITouch touch)
        {
            return true;
        }   
    }

This is what I use. Not sure it's the only possible way but works with no problems:

    protected void InitRecognizers()
    {
        var _swiperRight = new UISwipeGestureRecognizer();
        _swiperRight.Direction = UISwipeGestureRecognizerDirection.Right;
        _swiperRight.AddTarget(this, new MonoTouch.ObjCRuntime.Selector("SwipeNext"));
        this.View.AddGestureRecognizer(_swiperRight);
    }

    [Export("SwipeNext")]
    public virtual void Swipe(UIGestureRecognizer rec)
    {
        //your code here
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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