简单手势判断(二)
程序员文章站
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;
}
推荐阅读
-
简单手势判断(二)
-
unity 简单手势判断(一)
-
查找算法系列之简单查找:顺序查找、二分查找、分块查找
-
牛客网刷题java之在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。输入这样的一个二维数组和一个整数,判断数组中是否含有该整数
-
C在一段有序数组中判断一个数是否在这个数组中(二分查找)
-
java数据结构----图的遍历应用举例:编程实现判断一个有向图中任意给定的两个顶点之间是否存在一条长度为k的简单路径
-
判断素数最简单的写法
-
简单易懂的Java判断质数方法
-
Python判断质数的简单方法
-
MongoDB最简单的入门教程之二 使用nodejs访问MongoDB mongoDB数据库nodejs