顺时针旋转UIView大于180度的angular度

我正在将时钟臂指向12点到当前时间。 如果是11点,我想让arm顺时针旋转到11点的位置。 但是当然如果我使用:

CGAffineTransform rotation = CGAffineTransformMakeRotation(2*M_PI*11/12); [UIView animateWithDuration:3.0 animations:^{ clockArm.transform = rotation; }]; 

旋转逆时针旋转。 我试过了:

 CGFloat angle = 2*M_PI*11/12; CGAffineTransform firstRotation = CGAffineTransformMakeRotation(M_PI-0.001); CGFloat firstRotationTime = 3.0*(M_PI/angle); CGAffineTransform secondRotation = CGAffineTransformMakeRotation(angle); CGFloat secondRotationTime = 3.0 - firstRotationTime; [UIView animateWithDuration:firstRotationTime delay:0.0 options:UIViewAnimationCurveLinear animations:^{ self.clockArm1.transform = firstRotation; } completion:^(BOOL finished) { [UIView animateWithDuration:secondRotationTime delay:0.0 options:UIViewAnimationCurveLinear animations:^{ self.clockArm1.transform = secondRotation; } completion:^(BOOL finished){ }]; }]; 

animation确实是顺时针旋转的,但是它是波涛汹涌的 – animation似乎还在为第一个animation做一个UIViewAnimationEaseInOut。 我做错了什么,还是有另一种方法来实现我想要的?

当animation完成后,您可以使用CATransaction的完成块来设置视图的旋转属性。 以下函数在我的testing用例中工作:

 - (void) rotateViewAnimated:(UIView*)view withDuration:(CFTimeInterval)duration byAngle:(CGFloat)angle { [CATransaction begin]; CABasicAnimation *rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.byValue = [NSNumber numberWithFloat:angle]; rotationAnimation.duration = duration; rotationAnimation.removedOnCompletion = YES; [CATransaction setCompletionBlock:^{ view.transform = CGAffineTransformRotate(view.transform, angle); }]; [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; [CATransaction commit]; } 

你用它就好

 [self rotateViewAnimated:self.clockArm1 withDuration:3.0 byAngle:2*M_PI*11./12.]; 

感谢@Martin R接受的答案。 我编辑了一下:

我replace了这条线

 rotationAnimation.removedOnCompletion = YES; 

有这两行:

 rotationAnimation.removedOnCompletion = NO; rotationAnimation.fillMode = kCAFillModeForwards; 

然后,在CompletionBlock中,我又添加了一行:

 [view.layer removeAllAnimations]; 

所以,代码终于变成:

  - (void) rotateViewAnimated:(UIView*)view withDuration:(CFTimeInterval)duration byAngle:(CGFloat)angle { [CATransaction begin]; CABasicAnimation *rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.byValue = [NSNumber numberWithFloat:angle]; rotationAnimation.duration = duration; // changed by me rotationAnimation.removedOnCompletion = NO; rotationAnimation.fillMode = kCAFillModeForwards; [CATransaction setCompletionBlock:^{ view.transform = CGAffineTransformRotate(view.transform, angle); // added by me [view.layer removeAllAnimations]; // this is important }]; [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; [CATransaction commit]; } 

我的参考资料来自:

1, https://stackoverflow.com/a/3586433/2481444

2, 使用CABasicAnimation旋转CALayer后,图层跳回到未旋转的位置

正如在评论中提到的,试试这个iPhone UIImageView旋转或UIView无限360度旋转animation?

 #import <QuartzCore/QuartzCore.h> - (void) runSpinAnimationWithDuration:(CGFloat) duration; { CABasicAnimation* rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ]; rotationAnimation.duration = duration; rotationAnimation.cumulative = YES; rotationAnimation.repeatCount = 1.0; rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; [myView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; }