1

我想实现一个页面翻转动画使用UIPanGestureRecognizer。代码如下:UIPanGestureRecognizer - translationInView与CATransform3D

- (void)handlePan:(UIPanGestureRecognizer *)recognizer { 

    CGPoint location = [recognizer locationInView:self.view]; 
    CGPoint translation = [recognizer translationInView:self.view]; 
    CGPoint velocity = [recognizer velocityInView:self.view]; 

    [recognizer setTranslation:CGPointZero inView:recognizer.view]; 

switch (recognizer.state) { 
     case UIGestureRecognizerStateChanged: 
     { 

      self.currentTranslation = self.currentTranslation + translation.x; 

      //only pan left 
      if (self.currentTranslation > 0.0) { 
       self.currentTranslation = 0.0; 
      } 


      CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity; 
      rotationAndPerspectiveTransform.m34 = 1.0/-2000; 

      self.currentRotation = self.currentTranslation/2 * M_PI/180.0f; 
      //dont rotate past -90 degrees 
      if (self.currentRotation <= -M_PI/2) { 
       self.currentRotation = -M_PI/2+0.0001; 
      } 
      rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, self.currentRotation, 0.0f, 1.0f, 0.0f); 

      float ypos = self.view.layer.position.y; 

      self.view.layer.anchorPoint = CGPointMake(0, 0.5); 
      self.view.layer.position = CGPointMake(0, ypos);   
      self.view.layer.transform = rotationAndPerspectiveTransform; 


      break; 
     } 


     default: 
      break; 
    } 

} 

该动画起作用,并且当我向左拖动页面时,该页面将变为。但是,即使我以这种姿态稳步缓慢地移动,页面开始变得更快。我想让页面在触摸移动时“线性”旋转,就像Flipboard应用程序一样。我怎样才能控制翻译,因为它没有得到加速?

回答

3

翻译是积累的。在大小写UIGestureRecognizerStateChanged之后将此添加到您的代码中。

[recognizer setTranslation:CGPointZero inView:self.view]; 
+0

请注意,我已经在上面的代码中有这个。我认为这是一个更大的问题,随着角度变大,旋转效应会变得更加极端。我认为翻译本身是线性的。我只需要弄清楚如何抑制旋转。 – csoul

+1

@csoul我认为你的self.currentTranslation是累积的。请尝试使用translation.x,而不是将其添加到CATransform3DRotate的self.currentTranslation中。我相信这是问题所在。 – John