iOS – 在使用UIPanGestureRecognizer进行平移时实时推进animation

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

在现实世界中,您将手指放在地球上并将其移到右侧,当您移动手指时,地球旋转到右侧。

在iPhone上并不那么简单…

它可以是一个简单的手指在屏幕上下来,X点被抓住,然后当手指向右移动一个像素时,地球向右旋转一帧,原点变为新的点。 然后,如果手指移回到原始位置,则地球旋转一帧到左侧。 所有拿出你的手指…

那么我该怎么办呢? 我认为有一个“whileTouching”事件会持续运行/每500毫秒/等…

任何人都知道这样的一些示例代码?

编辑 :推进框架本身,我可以pipe理只是捕捉触摸事件我无法弄清楚。

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

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

- (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. } } 

这听起来像你应该使用UIPanGestureRecognizer来做到这一点。 基本上这将跟踪你的手指按下,只要你的手指按下,它是在一个特定的视图内的翻译。

编码的一个简单的想法是这样的:

 UIPanGestureRecognizer *touch = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(someFunction:); [self.view addGestureRecognizer:touch]; [touch release]; 

这将添加手势识别器到你的视图(假设这个代码是在视图控制器中)。 那么你需要在函数“someFunction”中添加“globe旋转”代码。

像这样的东西:

 -(void) someFunction:(UIPanGestureRecognizer *)recognizer { CGPoint translation = [recognizer translationInView:self.view]; // Your globe rotation code goes here } 

[识别器translationInView:self.view]会给你的手势识别器的翻译。 您可以使用它来设置您的地球的图像或变换,但是您正在处理实际的旋转。

希望这可以帮助。

干杯。