UIScreenEdgePanGestureRecognizer 在 ShouldBegin 的第一个 No 之后停止工作

UIScreenEdgePanGestureRecognizer stops working after first No from ShouldBegin

我有 UIScreenEdgePanGestureRecognizer 和自定义委托来决定手势是否应该开始。在 gestureRecognizerShouldBegin return No 之前一切正常。在代表继续被要求 gestureRecognizerShouldBegin 但手势不会独立于结果启动之后。

我试图调用 reset() 并删除手势识别器并将其重新添加到视图,但它不起作用。

我真的不知道如何进行,非常感谢任何指示。

P.S。我发现聚焦 UITextView 导致 - 在聚焦和不聚焦后 UITextView 手势停止工作。但我仍然不知道根本原因是什么。

我制作了一个简单的重现应用程序:https://drive.google.com/file/d/1IYPDdKILBxFCrjV3-RjrnerXPoWT0RQJ/view?usp=sharing

这是代码示例:

public class ContentPageRenderer : PageRenderer
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        var pan = new UIScreenEdgePanGestureRecognizer(HandlePan)
        {
            Edges = UIRectEdge.Left,
            Delegate = new InteractivePopGestureDelegate()
        };
        View.AddGestureRecognizer(pan);
    }

    private void HandlePan(UIScreenEdgePanGestureRecognizer recognizer)
    {
        Console.WriteLine($"HandlePan: {recognizer.State}"); // This stop being called after focusing Editor on page.
    }

    private class InteractivePopGestureDelegate : UIGestureRecognizerDelegate
    {
        public override bool ShouldBegin(UIGestureRecognizer recognizer)
        {
            Console.WriteLine("ShouldBegin"); // This gets called as expected.
            return true;
        }
    }
}

谢谢!

我不确定为什么共享代码不起作用,但我知道将 AddTarget 用于 UIScreenEdgePanGestureRecognizer 可以使其起作用。你可以试试。

示例代码:

public class ModalPageRenderer : PageRenderer
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        var pan = new UIScreenEdgePanGestureRecognizer();
        pan.Edges = UIRectEdge.Left;
        pan.AddTarget(() => HandlePan(pan));
        View.AddGestureRecognizer(pan);
    }

    private void HandlePan(UIScreenEdgePanGestureRecognizer recognizer)
    {
        Console.WriteLine($"HandlePan: {recognizer.State}"); // This stop being called after focusing Editor on page.

        if(recognizer.State == UIGestureRecognizerState.Began)
        {
            Console.WriteLine("ShouldBegin");
        }

        #region Just to make something happen on the UI
        // Following lines could be totally ignored / removed.
        // Just to make it visible on the UI.
        Color fadeColor = Color.FromRgb(122, 85, 191);

        nfloat Fade(double component, nfloat opacity) => (nfloat) (1d - opacity * (1d - component));

        var point = recognizer.LocationInView(recognizer.View);
        if (recognizer.State == UIGestureRecognizerState.Changed)
        {
            var opacity = point.X / recognizer.View.Frame.Width;
            recognizer.View.BackgroundColor =
                UIColor.FromRGB(
                    Fade(fadeColor.R, opacity),
                    Fade(fadeColor.G, opacity),
                    Fade(fadeColor.B, opacity));
        }
        else if (recognizer.State == UIGestureRecognizerState.Ended ||
                 recognizer.State == UIGestureRecognizerState.Cancelled)
        {
            recognizer.View.BackgroundColor = UIColor.White;
        }
        #endregion
    }
}