2

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

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

在 iPhone 上,事情没那么简单……

它可能是简单的事情,就像手指在屏幕上向下并抓住 X 点,然后当手指向右移动一个像素时,地球会向右移动一帧,并且原点会更改为新点。然后,如果手指移回原来的位置,地球会向左旋转一帧。一切都无需拿起你的手指...

那么我该怎么做呢?我假设有一个“whileTouching”事件会持续/每 500 毫秒/等运行一次……

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

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

4

2 回答 2

1

听起来您应该使用 UIPanGestureRecognizer 来执行此操作。基本上,这将跟踪您的手指按压,只要您的手指被按压,它就会在特定视图中进行翻译。

编码的简要想法类似于:

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

这会将手势识别器添加到您的视图中(假设此代码在视图控制器中)。然后你需要在函数“someFunction”中添加“地球旋转”代码。

像这样的东西:

-(void) someFunction:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self.view]; 

    // Your globe rotation code goes here
}

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

希望这可以帮助。

干杯。

于 2011-03-08T21:02:11.387 回答
1

每当手指移动时,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.
    }
}
于 2011-03-08T22:46:10.400 回答