Xamarin.iOS:TouchEvent不能在UIScrollView中调用

时间:2018-10-11 06:47:18

标签: xamarin.ios

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您可以自定义继承自UIScrollView的子类。并覆盖方法TouchesBegan TouchesBeganTouchesMoved

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
//. . .

现在您可以在TouchesBegan

中调用方法TouchesBegan TouchesMovedViewController
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
     // do some you want
}