如何在Xamarin中同时检测TouchEvent和TapGesture?

时间:2018-08-07 09:35:14

标签: xamarin xamarin.forms xamarin.android touch gesture

我在Android中像下面这样查看并进行着陆和移动事件。

public class AndroidView : View
{
    public override bool OnTouchEvent(MotionEvent e)
    {
        switch(e.Action)
        {
            case MotionEventActions.Down:
                DownAction();
                break;
            case MotionEventActions.Move:
                MoveAction();
                break;

        }
        return true; // this stops parent view gestures
    }
}

我在这里以xamarin形式进行点击手势

 var tapGestureRecognizer = new TapGestureRecognizer();
        tapGestureRecognizer.Tapped += (s, e) => {
            // handle the tap
        };
 view.GestureRecognizers.Add(tapGestureRecognizer);

我的问题是

  1. 当我在ontouch事件中返回false时,触摸移动不被调用
  2. 当我返回true时,则不会调用点击手势。

我的要求是,我需要同时获得触摸和点击手势事件

预先感谢

1 个答案:

答案 0 :(得分:0)

尝试一下。

public class AndroidView : View
{
    public override bool OnTouchEvent(MotionEvent e)
    {
        switch(e.Action)
        {
            case MotionEventActions.Down:
                this.Parent.RequestDisallowInterceptTouchEvent(true);
                DownAction();
                break;
            case MotionEventActions.Move:
                MoveAction();
                this.Parent.RequestDisallowInterceptTouchEvent(false);
                break;

        }
        return true; // Return true only if you want to handle touch otherwise, return base value. 
    }
}