FairGUI 手指事件

一、滑动手势事件。通过 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.trigger = 0.5f;
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);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值