【问题标题】:UIScreenEdgePanGestureRecognizer stops working after first No from ShouldBeginUIScreenEdgePanGestureRecognizer 在来自 ShouldBegin 的第一个 No 后停止工作
【发布时间】:2021-02-03 00:27:13
【问题描述】:

我有UIScreenEdgePanGestureRecognizer 和自定义委托来决定是否应该开始手势。在gestureRecognizerShouldBegin 返回No 之前一切正常。之后,委托继续被调用 gestureRecognizerShouldBegin,但手势不会独立于结果而开始。

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

我真的不知道如何进行,任何方向都非常感谢。

附:我发现聚焦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;
        }
    }
}

谢谢!

【问题讨论】:

  • 您好,如果您可以分享一些代码或示例项目链接来解释该问题,检查如何解决该问题会有所帮助。
  • @JuniorJiang-MSFT 我已经添加了 repro 应用的链接。
  • 感谢分享。我找到了解决方案,稍后会在答案中更新。

标签: ios xamarin.ios uigesturerecognizer


【解决方案1】:

我不确定为什么共享代码不起作用,但我知道将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
    }
}

【讨论】:

  • 谢谢!工作得很好,我应该考虑尝试这个,因为我正在寻找其他手势识别器,它们甚至没有选择在 ctor 中传递处理程序..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-06
  • 1970-01-01
  • 1970-01-01
  • 2015-08-13
  • 2020-03-25
  • 2018-01-05
  • 2015-03-26
相关资源
最近更新 更多