【问题标题】:Xamarin.iOS:TouchEvent cannot been called in UIScrollViewXamarin.iOS:无法在 UIScrollView 中调用 TouchEvent
【发布时间】:2018-10-11 06:47:17
【问题描述】:

我需要将 TouchEvent 添加到 UIScrollView 中,但是当我触摸 ScrollView 时似乎永远不会调用方法 TouchesBegan。我从 here 找到了解决方案。我必须添加一个 singleTapGesture 。但是,我可以出于某种原因不要这样做。这是唯一的解决方案吗?

【问题讨论】:

    标签: xamarin.ios


    【解决方案1】:

    您可以自定义一个继承自UIScrollView的子类。并覆盖方法TouchesBeganTouchesBeganTouchesMoved

    public class MyScrollerView: UIScrollView
    {
        public MyScrollerView()
        {
    
        }
        public override void TouchesBegan(NSSet touches, UIEvent evt)
        {
            this.NextResponder.TouchesBegan(touches, evt);
        }
    
        public override void TouchesMoved(NSSet touches, UIEvent evt)
        {
            this.NextResponder.TouchesMoved(touches, evt);
        }
    
        public override void TouchesEnded(NSSet touches, UIEvent evt)
        {
            this.NextResponder.TouchesEnded(touches, evt);
        }
    
    } 
    

    在 xxx.cs 中

    var myScrollView = new MyScrollerView();
    myScrollView.UserInteractionEnabled = true;
    myScrollView.DelaysContentTouches = false; // it is important
    //. . .
    

    现在您可以在ViewController 中调用TouchesBegan TouchesBeganTouchesMoved 方法

    public override void TouchesBegan(NSSet touches, UIEvent evt)
    {
         // do some you want
    }
    

    【讨论】:

      【解决方案2】:

      您可以在任何视图上添加手势识别器,这样做:

      myView.AddGestureRecognizer(new UITapGestureRecognizer(() =>
      {
        //Do Something
      }));
      

      【讨论】:

      • 感谢您的回答。正如我所说,我无法添加 TapGesture
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 2023-03-31
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多