iOS 360度无限旋转

iOS 360 endless rotation

我有这个代码可以无限旋转 UIImageView

            [UIView animateWithDuration:0.3f
                                  delay:0.0f
                                options:UIViewAnimationOptionCurveLinear|UIViewAnimationOptionRepeat
                             animations: ^{
                                 self.spinner.transform = CGAffineTransformRotate(CGAffineTransformIdentity, -M_PI);
                             }
                             completion: ^(BOOL finished) {
                             }];

但是我首先称这个方法为图像,它顺时针旋转而不是逆时针旋转。如果我在图像旋转时重新调用此方法,它会改变方向并开始逆时针旋转。

想法?

来自文档:

In iOS, a positive value specifies counterclockwise rotation and a negative value specifies clockwise rotation.

  CGAffineTransformRotate(CGAffineTransformIdentity, -M_PI)

更新: M_PI 是常量,不受负值影响。不知何故 -M_PI 最后还是被当成正数了!!!

如果你给-180它会顺时针旋转,如果你给180它会逆时针旋转

改用 CABasicAnimation,因为它更强大。您只需调用以下代码片段一次,动画将 运行 无限期地运行:

CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotate.toValue = @(M_PI * 2); // use @(-M_PI * 2) for counter clockwise
rotate.duration = 0.3;
rotate.cumulative = true;
rotate.repeatCount = HUGE_VALF;
[self.spinner.layer addAnimation:rotate forKey:@"rotateAnim"];

Swift:

let rotate = CABasicAnimation(keyPath: "transform.rotation.z")
rotate.toValue = M_PI * 2
rotate.duration = 0.3
rotate.cumulative = true
rotate.repeatCount = HUGE
self.spinner.layer.addAnimation(rotate, forKey: "rotateAnim")