不久前我遇到了同样的问题。我不记得这个问题的原因,但这是我的解决方案:
/**
* Incrementally rotated the arrow view by a given angle.
*
* @param degrees Angle in degrees.
* @param duration Duration of the rotation animation.
*/
- (void)rotateArrowViewByAngle:(CGFloat)degrees
withDuration:(NSTimeInterval)duration {
CABasicAnimation *spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
spinAnimation.fromValue = [NSNumber numberWithFloat:self.currentAngle / 180.0 * M_PI];
spinAnimation.toValue = [NSNumber numberWithFloat:degrees / 180.0 * M_PI];
spinAnimation.duration = duration;
spinAnimation.cumulative = YES;
spinAnimation.additive = YES;
spinAnimation.removedOnCompletion = NO;
spinAnimation.delegate = self;
spinAnimation.fillMode = kCAFillModeForwards;
[self.arrowView.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
self.currentAngle = degrees;
}
然后你可以使用委托方法
- (void)animationDidStart:(CAAnimation *)theAnimation
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
旋转保持旋转。此外,程度和持续时间参数可能是非常高的数字......如果这足够了。
更新:
正如银口所说,
spinAnimation.repeatCount = HUGE_VALF; // HUGE_VALF is defined in math.h so import it
比在委托中重新启动动画要好得多。
请注意:
self.currentAngle 是一个记住当前最终旋转的属性。
我需要它来使视图左右旋转。