2016-12-09 54 views
2

我在視圖中使用了下面的3dRotation函數。但我不想傾斜/翻轉上看來,我只想左/右/上/下運動上的看法。如何避免iOS上的CATransform3D旋轉傾斜/翻轉?

我如何避免傾斜和翻轉我的看法?

- (void)Move3dPan:(UIPanGestureRecognizer *)gesture 
{ 
    if (gesture.state == UIGestureRecognizerStateChanged) 
    { 
     CGPoint displacement = [gesture translationInView:self.view]; 
     CATransform3D currentTransform = self.popUpView.layer.sublayerTransform; 

     if (displacement.x==0 && displacement.y==0) 
     { 
      // no rotation, nothing to do 
      return; 
     } 

     CGFloat totalRotation = sqrt(displacement.x * displacement.x + displacement.y * displacement.y) * M_PI/360.0; 
     CGFloat xRotationFactor = displacement.x/totalRotation; 
     CGFloat yRotationFactor = displacement.y/totalRotation; 

     CATransform3D rotationalTransform = CATransform3DRotate(currentTransform, totalRotation, 
                  (xRotationFactor * currentTransform.m12 - yRotationFactor * currentTransform.m11), 
                  (xRotationFactor * currentTransform.m22 - yRotationFactor * currentTransform.m21), 
                  (xRotationFactor * currentTransform.m32 - yRotationFactor * currentTransform.m31)); 

     [CATransaction setAnimationDuration:0]; 

     self.popUpView.layer.sublayerTransform = rotationalTransform; 

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

這是斯威夫特,但應該有助於瞭解如何旋轉,在2D移動和縮放層:http://stackoverflow.com/a/34438890/1694526 – sketchyTech

回答

1
-(void) startAnimatingCupView 

{ 

    [self stopAnimatingCupView]; 

    timer = [NSTimer scheduledTimerWithTimeInterval:0.8 repeats:true block:^(NSTimer * _Nonnull timer) { 

     [self animateCupView]; 

    }]; 

} 


-(void) animateCupView{ 

    [UIView animateWithDuration:0.4 animations:^{ 

     self.imgCup.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(180)); 

    } completion:^(BOOL finished) { 

     [UIView animateWithDuration:0.4 animations:^{ 

      self.imgCup.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(360)); 

     }]; 

    }]; 

} 


-(void) stopAnimatingCupView{ 

    if (timer != nil) { 

     [timer invalidate]; 

     timer = nil; 

    } 

    [self.imgCup.layer setTransform:CATransform3DIdentity]; 

} 

我希望這將工作

+0

感謝您的回答。 我想使用UIPanGestureRecognizer進行觸摸移動時的3drotation。 –