滑动手势事件,可以通过OnBegin+OnMove+OnEnd来关心滑动的结果,也可以通过OnAction来得到结果,结果有角度和方向
SwipeGesture gesture1 = new SwipeGesture(holder);
gesture1.onMove.Add(OnSwipeMove);
gesture1.onEnd.Add(OnSwipeEnd);
void OnSwipeMove(EventContext context)
{
SwipeGesture gesture = (SwipeGesture)context.sender;
Vector3 v = new Vector3();
if (Mathf.Abs(gesture.delta.x) > Mathf.Abs(gesture.delta.y))
{
v.y = -Mathf.Round(gesture.delta.x);
if (Mathf.Abs(v.y) < 2) //消除手抖的影响
return;
}
else
{
v.x = -Mathf.Round(gesture.delta.y);
if (Mathf.Abs(v.x) < 2)
return;
}
_ball.Rotate(v, Space.World);
}
void OnSwipeEnd(EventContext context)
{
SwipeGesture gesture = (SwipeGesture)context.sender;
Vector3 v = new Vector3();
if (Mathf.Abs(gesture.velocity.x) > Mathf.Abs(gesture.velocity.y))
{
v.y = -Mathf.Round(Mathf.Sign(gesture.velocity.x) * Mathf.Sqrt(Mathf.Abs(gesture.velocity.x)));
if (Mathf.Abs(v.y) < 2)
return;
}
else
{
v.x = -Mathf.Round(Mathf.Sign(gesture.velocity.y) * Mathf.Sqrt(Mathf.Abs(gesture.velocity.y)));
if (Mathf.Abs(v.x) < 2)
return;
}
GTween.To(v, Vector3.zero, 0.3f).SetTarget(_ball).OnUpdate(
(GTweener tweener) =>
{
_ball.Rotate(tweener.deltaValue.vec3, Space.World);
});
}
长按手势,当按时一定时间后,通过OnAction来派发事件,如果once为false时,则间断N时间来派发OnAction;
LongPressGesture gesture2 = new LongPressGesture(holder);
gesture2.once = false;
gesture2.onAction.Add(OnHold);
void OnHold(EventContext context)
{
GTween.Shake(_ball.transform.localPosition, 0.05f, 0.5f).SetTarget(_ball).OnUpdate(
(GTweener tweener) =>
{
_ball.transform.localPosition = new Vector3(tweener.value.x, tweener.value.y, _ball.transform.localPosition.z);
});
}
两个手指的捏放操作
PinchGesture gesture3 = new PinchGesture(holder);
gesture3.onAction.Add(OnPinch);
void OnPinch(EventContext context)
{
GTween.Kill(_ball);
PinchGesture gesture = (PinchGesture)context.sender;
float newValue = Mathf.Clamp(_ball.localScale.x + gesture.delta, 0.3f, 2);
_ball.localScale = new Vector3(newValue, newValue, newValue);
}
手指反向操作
RotationGesture gesture4 = new RotationGesture(holder);
gesture4.onAction.Add(OnRotate);
void OnRotate(EventContext context)
{
GTween.Kill(_ball);
RotationGesture gesture = (RotationGesture)context.sender;
_ball.Rotate(Vector3.forward, -gesture.delta, Space.World);
}