iOS - 使用 UIPanGestureRecognizer 平移时实时推进动画 [英] iOS - Advancing an Animation in Realtime While Panning with UIPanGestureRecognizer

查看:102
本文介绍了iOS - 使用 UIPanGestureRecognizer 平移时实时推进动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想复制地球的旋转.

Basically I want to replicate the spinning of a globe.

在现实世界中,您将手指放在地球上并将其向右移动,而当您移动手指时,地球会向右旋转.

In the real world you'd put your finger on the globe and and move it to the right and while you are moving your finger the globe spins to the right.

在 iPhone 上没那么简单...

On an iPhone it's not that simple...

这可能很简单,比如手指放在屏幕上并抓住 X 点,然后当手指向右移动一个像素时,地球仪向右旋转一帧,原点更改为新点.然后,如果手指移回原始位置,地球将向左旋转一帧.无需拿起您的手指...

It could be something as simple as a finger comes down on the screen and point X is grabbed and then when the finger moves one pixel to the right the globe spines one frame to the right and the origin changes to the new spot. Then if the finger moves back to the original spot the globe spin one frame to the left. All with out picking up your finger...

那我该怎么做呢?我假设有一个whileTouching"事件会不断/每 500 毫秒/等等...

So how might I go about this? I assume there is a "whileTouching" event that would run constantly / every 500ms / etc...

有人知道这样的示例代码吗?

Anyone know of some sample code like this?

编辑:推进帧本身我可以管理我无法弄清楚的触摸事件的捕获.

Edit: The advancing the frame itself I can manage just the capturing of the touch event I can't figure out.

推荐答案

每当手指移动时,UIPanGestureRecognizer 将继续调用其动作方法.您使用状态来确定如何更改当前视图.

The UIPanGestureRecognizer will continue to call its action methods whenever the finger moves. You use the state to determine how to change the current view.

此代码示例假定视图的视图控制器处理手势.

This code sample assumes that the view's view controller handles the gesture.

- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture //Your action method
{
    switch(panGesture.state) {
        case UIGestureRecognizerStateChanged:
            CGPoint translation = [panGesture translationInView:self.view];
            // Rotate the globe by the amount in translation
            // Fall through to began so that the next call is relative to this one
        case UIGestureRecognizerStateBegan:
            [panGesture setTranslation:CGPointZero inView:self.view];
            break;
        case UIGestureRecognizerStateEnded:
            CGPoint velocity = [panGesture velocityInView:self.view];
            // The user lifted their fingers. Optionally use the velocity to continue rotating the globe automatically
            break;
        default:
            // Something else happened. Do any cleanup you need to.
    }
}

这篇关于iOS - 使用 UIPanGestureRecognizer 平移时实时推进动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆