欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简单手势判断(二)

程序员文章站 2024-03-16 20:45:58
...

在移动设备上经常会有手势操作:方向滑动判断,缩放,乃至稍微复杂的一些,画圈,S移动等;当然目前有很多手势插件可以用,不过能自己弄出来还是不错的,先前有一个简单的方向滑动,这里新增一个简单的缩放判断,希望对你有帮助

--------------------------------------------------------------------我是分割线---------------------------------------------------------------------------------------------------------


说明:需要开启多点触碰,放在初始化执行就可以了,中间注释的那一块是得到滑动的增量,需要的的可以自行打开;

        //开启多点触屏
        Input.multiTouchEnabled = true;


 #region---------------------手势判断----------------------

    enum FingerTouchType
    {
        type_LeftMove,
        type_RightMove,
        type_UpMove,
        type_DownMove,
        type_ScaleMin,
        type_ScaleMax
    }

    //记录单个触屏位置
    Vector2 screenPos = new Vector2();
    //手指位置
    Vector2 finger1 = new Vector2(), finger2 = new Vector2();
    //手指移动距离
    Vector2 mov1 = new Vector2(), mov2 = new Vector2();
    //距离
    float distance1 = 0, distance2 = 0;

    /// <summary>
    /// 判断函数,放在update里执行
    /// </summary>
    void FingerTouch()
    {
        if (Input.touchCount <= 0)
            return;
        if (Input.touchCount == 1)
        {
            if (Input.touches[0].phase == TouchPhase.Began)
            {
                //记录位置
                screenPos = Input.touches[0].position;

            }
            else if (Input.touches[0].phase == TouchPhase.Moved)
            {

            }

            if (Input.touches[0].phase == TouchPhase.Ended && Input.touches[0].phase != TouchPhase.Canceled)
            {
                Vector2 pos = Input.touches[0].position;
                switch (CheckTouchMove(screenPos, pos))
                {
                    case FingerTouchType.type_LeftMove:
                        {
                            touchEnd.text = "左滑";
                            break;
                        }
                    case FingerTouchType.type_RightMove:
                        {
                            touchEnd.text = "右滑";
                            break;
                        }
                    case FingerTouchType.type_UpMove:
                        {
                            touchEnd.text = "上滑";
                            break;
                        }
                    case FingerTouchType.type_DownMove:
                        {
                            touchEnd.text = "下滑";
                            break;
                        }
                }
            }
        }
        else if (Input.touchCount > 1)
        {
            for (int i = 0; i < Input.touchCount; ++i)
            {
                Touch touch = Input.touches[i];
                if (touch.phase == TouchPhase.Ended)
                    break;
                if (touch.phase == TouchPhase.Began)
                {
                    if (i == 0)
                        finger1 = touch.position;
                    else
                        finger2 = touch.position;
                    
                    distance1 = Vector2.Distance(finger1, finger2);
                  
                }

                #region--------------------滑动检测-----------------------
                /*if (touch.phase == TouchPhase.Moved)
                {
                    float mov = 0;
                    if (i == 0)
                    {
                        finger1 = touch.position;
                        mov1 = touch.deltaPosition;
                    }
                    else
                    {
                        finger2 = touch.position;
                        mov2 = touch.deltaPosition;
                        if (finger1.x > finger2.x)
                        {
                            mov = mov1.x;
                        }
                        else
                        {
                            mov = mov2.x;
                        }

                        if (finger1.y > finger2.y)
                        {
                            mov += mov1.y;
                        }
                        else
                        {
                            mov += mov2.y;
                        }
                    }
                }*/
                #endregion


                if (touch.phase == TouchPhase.Moved)
                {
                    if (i == 0)
                        finger1 = touch.position;
                    else
                        finger2 = touch.position;
                       
                    distance2 = Vector2.Distance(finger1, finger2);
                   
                    if (CheckTouchScale(distance1, distance2) == FingerTouchType.type_ScaleMax)
                    {
                        touchEnd.text = "放大";
                        Debug.Log("max");
                    }
                    else
                    {
                        touchEnd.text = "缩小";
                        Debug.Log("min");
                    }

                }
            }
        }
    }

    FingerTouchType CheckTouchMove(Vector2 start, Vector2 end)
    {
        if (Mathf.Abs(start.x - end.x) > Mathf.Abs(start.y - end.y))
        {
            if (start.x > end.x)
            {
                //左滑
                return FingerTouchType.type_LeftMove;
            }
            else
            {
                //右滑
                return FingerTouchType.type_RightMove;
            }
        }
        else//垂直滑动
        {
            if (start.y > end.y)
            {
                //下滑
                return FingerTouchType.type_DownMove;
            }
            else
            {
                //上滑
                return FingerTouchType.type_UpMove;
            }
        }
    }

    


    FingerTouchType CheckTouchScale(float dis1, float dis2)
    {
        return Mathf.Abs(dis1) - Mathf.Abs(dis2) >= 0 ? FingerTouchType.type_ScaleMin : FingerTouchType.type_ScaleMax;
    }


    #endregion


 #region---------------------鼠标判断----------------------

    void CheckMouseControl()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //记录位置
            screenPos = Input.mousePosition;
        }

        if (Input.GetMouseButtonUp(0))
        {
            Vector2 pos = Input.mousePosition;
            float angle = GetAngleByTwoVector(screenPos, pos);
            Debug.Log("角度;" + angle);
            switch (CheckTouchMove(screenPos, pos))
            {
                case FingerTouchType.type_LeftMove:
                    {
                        touchEnd.text = "左滑";
                        break;
                    }
                case FingerTouchType.type_RightMove:
                    {
                        touchEnd.text = "右滑";
                        break;
                    }
                case FingerTouchType.type_UpMove:
                    {
                        touchEnd.text = "上滑";
                        break;
                    }
                case FingerTouchType.type_DownMove:
                    {
                        touchEnd.text = "下滑";
                        break;
                    }
            }
        }
    }

    #endregion

   
//角度
float GetAngleByTwoVector(Vector2 fromVector, Vector2 toVector)
    {
        Vector2 pos = new Vector2(fromVector.x, toVector.y);
        float angle = 0;
        Vector2 dic1 = fromVector - toVector;
        Vector2 dic2 = fromVector - pos;
        angle = Vector2.Dot(dic2.normalized, dic1.normalized);//点乘得到cos值
        angle = Mathf.Acos(angle) * 180 / 3.1416926f;
       
        return angle;
    }





相关标签: 2D Unity