UISwipeGestureRecognizer无法在iOS 4.3中运行

时间:2011-03-14 16:16:18

标签: iphone ios xamarin.ios ios4

此代码在以前的iOS版本中运行良好,但现在在iOS 4.3中它已不再有效。任何人都有什么需要纠正的建议吗?

在FinishedLaunching事件中,我调用AddSyncGesture,然后无论何时旋转设备,我都会有一个调用方法的通知程序以及方向更改的垂直更改,因此我必须删除并重新添加它。

        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;
        }   
    }

1 个答案:

答案 0 :(得分:0)

这就是我使用的。不确定这是唯一可能的方式,但没有问题:

    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
    }