2014-04-01 120 views
0

我想执行一些旋转动画。现在我这样做:ios旋转动画与CGAffineTransformRotate

#define degreesToRadians(x) (M_PI * x/180.0) 

[self.crossButton setTransform:CGAffineTransformRotate(self.crossButton.transform, degreesToRadians(-rotationDegree))]; 

但当我通过例如360度nothong发生。当我超过180的价值时,它开始不好。你知道我做错了什么吗?

回答

1

你的问题是'setTransformation'与矩阵旋转一起工作。出于这个原因,你将永远得到最终结果的最短路线。当你通过360度旋转时,你的物体在变形后将是相同的。出于这个原因,转型将无所作为,因为它已经是它应该结束的地方。对于180到360度之间的值,您的旋转将会“倒退”,因为再次。轮换使用“最短”路径来达到最终结果。

你可以试试这个代码:

UIView* toRotate = VIEW_TO_ROTATE; 
CGFloat degreesToRotate = DEGREES; 
CGFloat animationTime = TOTAL_ANIMATION_TIME; 


NSInteger intervals = ((int)degreesToRotate)/179.9; 
CGFloat rest = degreesToRotate-(intervals*179.9); 

CGFloat radInterval = degreesToRotate>=0?179.9:-179.9; 
CGFloat radRest = (M_PI * rest/180.0); 

CGFloat intervalTime = (1-(radRest/M_PI/2))/intervals; 
CGFloat restTime = (radRest/M_PI/2)/intervals; 

[UIView animateKeyframesWithDuration:animationTime 
           delay:0.0f 
          options:UIViewKeyframeAnimationOptionCalculationModeLinear 
          animations: 
^{ 
    for (int i=0; i<intervals; i++) { 
     [UIView addKeyframeWithRelativeStartTime:intervalTime*i relativeDuration:intervalTime animations:^{ 
      toRotate.transform = CGAffineTransformConcat(toRotate.transform, CGAffineTransformMakeRotation(radInterval)); 
     }]; 
    } 
    [UIView addKeyframeWithRelativeStartTime:intervalTime*intervals relativeDuration:restTime animations:^{ 
     toRotate.transform = CGAffineTransformConcat(toRotate.transform, CGAffineTransformMakeRotation(radRest)); 
    }]; 
} completion:^(BOOL finished) { 

}]; 

请务必将VIEW_TO_ROTATEDEGREESTOTAL_ANIMATION_TIME与价值观,你需要!