【问题标题】:iOS - Advancing an Animation in Realtime While Panning with UIPanGestureRecognizeriOS - 在使用 UIPanGestureRecognizer 平移时实时推进动画
【发布时间】:2011-03-08 20:47:44
【问题描述】:

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

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

在 iPhone 上没那么简单……

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

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

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

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

【问题讨论】:

    标签: iphone objective-c ios gesture-recognition swipe


    【解决方案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.
        }
    }
    

    【讨论】:

      【解决方案2】:

      听起来您应该使用 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] 将为您提供手势识别器的翻译。您可以使用它来设置地球的图像或变换,但是您正在处理实际的旋转。

      希望这会有所帮助。

      干杯。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 2014-12-28
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多