图层按方向旋转



我想要旋转一个CALayer,首先我把它旋转了-30度,这样它就向左了

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
self.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-30));
[UIView commitAnimations];

现在我想让它完成旋转,从左边开始再旋转一次,但它走的是最短路径

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
self.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-30));
[UIView commitAnimations];

如何强制它的旋转方向

您可以使用CABasicAnimation旋转到特定角度。

CABasicAnimation *rotate = 
    [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotate.toValue = @(M_PI*2); 
rotate.duration = 1.0; // your duration
[yourView.layer addAnimation:rotate 
                      forKey:@"myRotationAnimation"];

还有一些参数可以用来配置动画的外观(例如计时功能)。

默认情况下,动画将在完成后自行删除,所以你应该更新到最终值(这已经在所有"我如何旋转…"的问题中一遍又一遍地解释了)。

同样,你需要QuartzCore.framework的核心动画(CALayer和CABasicAnimation在这个例子中)

最新更新