判断手势在物品的滑动方向(上下左右)

Posted weiqiangwaideshijie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了判断手势在物品的滑动方向(上下左右)相关的知识,希望对你有一定的参考价值。

 /*************
** Company: DX
**SrtiptName:DragItem
** Auth:    CW
** Des:    文本描述
** Ver.:     V1.0.0
*************/

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using Kernal;

namespace Global

{
    public class DragItem : MonoBehaviour
    {
        /// <summary>
        /// 开始触摸的手指的位置
        /// </summary>
        private Vector3 _startTouchPos;
        private PointerEventData _pointerEventData;
        /// <summary>
        /// 拖拽的方向
        /// </summary>
        private E_DragDir _dragDir;
        void Start()
        {
            EventTriggerListener.Get(gameObject).OnMyBeginDrag += OnBeginDrag;
            EventTriggerListener.Get(gameObject).OnMyDrag += OnDrag;
            EventTriggerListener.Get(gameObject).OnMyEndDrag += OnEndDrag;

        }


        private void OnEndDrag(GameObject go, BaseEventData baseEventData)
        {
            _dragDir = E_DragDir.None;
        }
        private E_ClickPosType CurretClickType;
        private void OnDrag(GameObject go, BaseEventData baseEventData)
        {
            if (_pointerEventData.position.x > _startTouchPos.x)
            {
                Log.Debug("往右边滑动");
                _dragDir = E_DragDir.Right;

            }
            else if (_pointerEventData.position.x < _startTouchPos.x)
            {
                Log.Debug("往左边滑动");
                _dragDir = E_DragDir.Left;
              
            }
            else if (_pointerEventData.position.y < _startTouchPos.y)
            {
                 Log.Debug("往下面滑动");
                _dragDir = E_DragDir.Down;
            }
            else if (_pointerEventData.position.y > _startTouchPos.y)
            {
                Log.Debug("往上面滑动");
                _dragDir = E_DragDir.Up;
                
            }
            _startTouchPos = _pointerEventData.position;

        }

        private void OnBeginDrag(GameObject go, BaseEventData baseEventData)
        {

             _pointerEventData = baseEventData as PointerEventData;
            _startTouchPos = _pointerEventData.position;
        }

        /// <summary>
        /// 得到滑动的方向
        /// </summary>
        /// <returns></returns>
        public E_DragDir GetDragDir()
        {
            return _dragDir;
        }
    }
}

 

以上是关于判断手势在物品的滑动方向(上下左右)的主要内容,如果未能解决你的问题,请参考以下文章

ios 滑动手势 怎么判断左右滑动

android fragment怎么监听上下滑动

android 判断左右滑动,上下滑动的GestureDetector简单手势检测

关于手机端 手势滑动的方向的判断(方式一)

js判断移动端手势 上下左右滑动事件

平移手势和滑动手势