繁体   English   中英

使用CGAffineTransformRotate的ios旋转动画

[英]ios rotation animation with CGAffineTransformRotate

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

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

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

但是当我经过例如360度时,nothong发生了。 当我传递高于180的值时,它开始无法正常工作。 你知道我做错了什么吗?

您的问题是“ 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替换为VIEW_TO_ROTATE的值!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM