OnLongPress 不适用于我们的触摸事件 [英] OnLongPress is not working with our touch events

查看:54
本文介绍了OnLongPress 不适用于我们的触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个游戏,我想在用户单点触摸屏幕时执行某些操作,并在用户长按屏幕时执行一些其他操作.为此,我创建了一个手势检测器类并向它们添加事件.

I am developing a game in which I want to perform certain operations when user single touch the screen, and some other operations when user long press the screen. For this, I have created a Gesture Detector class and add events to them.

表面视图类

public MySurfaceView(Context context, IAttributeSet attrs):base(context, attrs)
    {
        this.context=context;
        SetWillNotDraw(false);
        gestureDetector = new GestureDetector(context, new GestureListener());

    }

 public override bool OnTouchEvent(MotionEvent e)
    {
        Log.Debug(Tag, "Inside" + System.Reflection.MethodBase.GetCurrentMethod().Name + "Method");
        return gestureDetector.OnTouchEvent(e); 
    }

手势监听类

  private class GestureListener : GestureDetector.SimpleOnGestureListener
    {
        public override bool OnDown(MotionEvent e)
        {
            Log.Debug("Tag", "Inside Gesture OnDown Event");
            // don't return false here or else none of the other 
            // gestures will work
            return true;

        }

        public override bool OnSingleTapConfirmed(MotionEvent e)
        {
            Log.Debug("Tag", "Inside Gesture OnSingleTapConfirmed Event");

            return true;
        }

        public override bool OnDoubleTap(MotionEvent e)
        {
            Log.Debug("Tag", "Inside Gesture OnDoubleTap Event");
            return true;
        }

        public override void OnLongPress(MotionEvent e)
        {
            Log.Debug("Tag", "Inside Long Press Event");
        }




    }

除 OnLongPress 之外的所有事件都使用上述代码.在阅读完这个问题的评论后.我必须为 OnDown 事件返回 false.根据评论更新我的代码后,我的 OnLongPress 事件开始工作,但现在只有 OnLongPress 事件在工作.

All events except OnLongPress is working with the above code. After going through the comments of this question. I have to return false for OnDown event. After updating my code base on the comment my OnLongPress event start working but now only OnLongPress event is working.

   public override bool OnDown(MotionEvent e)
        {
            Log.Debug("Tag", "Inside Gesture OnDown Event");
            // don't return false here or else none of the other 
            // gestures will work
            return false;

        }

有什么方法可以让 OnLongPress 与其他事件一起工作,因为我需要所有事件一起工作.

Is there any way by which I can make OnLongPress work with other events as I need all the events to work together.

推荐答案

将您的 OnTouchEvent 更改为以下内容:

Change your OnTouchEvent to the following:

 public override bool OnTouchEvent(MotionEvent e){
     Log.Debug(Tag, "Inside" + System.Reflection.MethodBase.GetCurrentMethod().Name + "Method");
     gestureDetector.OnTouchEvent(e); 
     return true;
}

您可以在此处找到说明.

这篇关于OnLongPress 不适用于我们的触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆