加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 检测滑动手势方向

发布时间:2020-12-15 19:28:01 所属栏目:百科 来源:网络整理
导读:这是我尝试和模拟滑动手势的代码,因此当我构建到移动设备时,我知道它会起作用.什么都没有记录,我很困惑为什么它似乎不起作用.我希望它在控制台中打印出来,我可以刷RTL(从右到左)或LTR(从左到右).我不明白我做错了什么. void Update(){ if (Input.GetMouseBut
这是我尝试和模拟滑动手势的代码,因此当我构建到移动设备时,我知道它会起作用.什么都没有记录,我很困惑为什么它似乎不起作用.我希望它在控制台中打印出来,我可以刷RTL(从右到左)或LTR(从左到右).我不明白我做错了什么.

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        startPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }
    if (Input.GetMouseButtonUp(0))
    {
        endPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }

    if (startPosition != endPosition && startPosition != Vector3.zero && endPosition != Vector3.zero)
    {
        float deltaX = endPosition.x - startPosition.x;
        float deltaY = endPosition.y - startPosition.y;
        if ((deltaX > 5.0f || deltaX < -5.0f) && (deltaY >= -1.0f || deltaY <= 1.0f))
        {
            if (startPosition.x < endPosition.x)
            {
                print("LTR");
            }
            else
            {
                print("RTL");
            }
        }
        startPosition = endPosition = Vector3.zero;
    }
}

解决方法

我可以在你的代码中发现一些问题.将Vector3与==或!=进行比较并不是一个好主意.近似比较很好.您正在移动平台上使用Input.GetMouseButtonDown.

您需要使用Input.touches来执行此操作.在它上面循环,在TouchPhase.Began中存储起始位置,然后在TouchPhase.Ended中存储结束位置.然后,您可以使用这两个变量来确定手指的方向.

下面的代码检测滑动方向,即使手指尚未在TouchPhase.Moved的帮助下释放.您可以通过启用detectSwipeOnlyAfterRelease布尔变量来禁用它.您还可以修改SWIPE_THRESHOLD以获得灵敏度.

public class SwipeDetector : MonoBehaviour
{
    private Vector2 fingerDown;
    private Vector2 fingerUp;
    public bool detectSwipeOnlyAfterRelease = false;

    public float SWIPE_THRESHOLD = 20f;

    // Update is called once per frame
    void Update()
    {

        foreach (Touch touch in Input.touches)
        {
            if (touch.phase == TouchPhase.Began)
            {
                fingerUp = touch.position;
                fingerDown = touch.position;
            }

            //Detects Swipe while finger is still moving
            if (touch.phase == TouchPhase.Moved)
            {
                if (!detectSwipeOnlyAfterRelease)
                {
                    fingerDown = touch.position;
                    checkSwipe();
                }
            }

            //Detects swipe after finger is released
            if (touch.phase == TouchPhase.Ended)
            {
                fingerDown = touch.position;
                checkSwipe();
            }
        }
    }

    void checkSwipe()
    {
        //Check if Vertical swipe
        if (verticalMove() > SWIPE_THRESHOLD && verticalMove() > horizontalValMove())
        {
            //Debug.Log("Vertical");
            if (fingerDown.y - fingerUp.y > 0)//up swipe
            {
                OnSwipeUp();
            }
            else if (fingerDown.y - fingerUp.y < 0)//Down swipe
            {
                OnSwipeDown();
            }
            fingerUp = fingerDown;
        }

        //Check if Horizontal swipe
        else if (horizontalValMove() > SWIPE_THRESHOLD && horizontalValMove() > verticalMove())
        {
            //Debug.Log("Horizontal");
            if (fingerDown.x - fingerUp.x > 0)//Right swipe
            {
                OnSwipeRight();
            }
            else if (fingerDown.x - fingerUp.x < 0)//Left swipe
            {
                OnSwipeLeft();
            }
            fingerUp = fingerDown;
        }

        //No Movement at-all
        else
        {
            //Debug.Log("No Swipe!");
        }
    }

    float verticalMove()
    {
        return Mathf.Abs(fingerDown.y - fingerUp.y);
    }

    float horizontalValMove()
    {
        return Mathf.Abs(fingerDown.x - fingerUp.x);
    }

    //////////////////////////////////CALLBACK FUNCTIONS/////////////////////////////
    void OnSwipeUp()
    {
        Debug.Log("Swipe UP");
    }

    void OnSwipeDown()
    {
        Debug.Log("Swipe Down");
    }

    void OnSwipeLeft()
    {
        Debug.Log("Swipe Left");
    }

    void OnSwipeRight()
    {
        Debug.Log("Swipe Right");
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读