//没有触摸
if (Input.touchCount <= 0)
{
return;
}
//单点拖动
else if (Input.touchCount == 1)
{
touch1 = Input.GetTouch(0);
if (touch1.phase == TouchPhase.Began)
{
offest = touch1.position - (Vector2)transform.position;
return;
}
if (touch1.phase == TouchPhase.Moved)
{
transform.position = touch1.position - offest;
}
}
//双指旋转 && 缩放
else if (2 <= Input.touchCount)
{
touch1 = Input.GetTouch(0);
touch2 = Input.GetTouch(1);
if (touch2.phase == TouchPhase.Began)
{
oldPos1 = touch1.position;
oldPos2 = touch2.position;
return;
}
if (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved)
{
Vector2 curDir = touch2.position - touch1.position;
Vector2 oldDir = oldPos2- oldPos1;
float angle = Vector2.Angle(oldDir, curDir);
angle *= Mathf.Sign(Vector3.Cross(oldDir, curDir).z);
transform.localRotation *= Quaternion.Euler(Vector3.forward * angle);
float oldDistance = Vector2.Distance(oldPos1, oldPos2);
float newDistance = Vector2.Distance(touch1.position, touch2.position);
float offset = newDistance - oldDistance;
Scale(offset);
oldPos1 = touch1.position;
oldPos2 = touch2.position;
}
}
触摸屏单指拖拽、双指缩放、双指旋转
最新推荐文章于 2024-07-29 21:55:23 发布