脚本记录:移动端图片扩大缩小

版权声明:转载请注明,谢谢 https://blog.csdn.net/qq_38655924/article/details/83381984
   if (Input.touchCount > 1)
        {
            //两次触摸都有滑动
            if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
            {
                //获取第一、二次两次触摸的位置
                Vector2 tempPosition1 = Input.GetTouch(0).position;
                Vector2 tempPosition2 = Input.GetTouch(1).position;
                //放大
                if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
                {
                    float oldScale = Images_Panel.transform.localScale.x;

                    float newScale = oldScale * 1.025f;

                    Images_Panel.transform.localScale = new Vector3(newScale, newScale, newScale);
                }
                else//缩小
                {
                    float oldScale = Images_Panel.transform.localScale.x;
                    float newScale = oldScale / 1.025f;
                    Images_Panel.transform.localScale = new Vector3(newScale, newScale, newScale);
                }

                //备份上一次触摸点的位置,用于对比   

                oldPosition1 = tempPosition1;

                oldPosition2 = tempPosition2;

            }

        }
bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
    {
        //函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势   
        var leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        var leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
        if (leng1 < leng2)
        {
            //放大手势   
            return true;
        }
        else
        {
            //缩小手势   
            return false;
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_38655924/article/details/83381984