在CABasicAnimation中闪烁以进行旋转

我正在试图从一个angular度旋转一个CAShapeLayer,只要按下一个button,它就会从当前angular度开始旋转。

我正在使用委托函数animationDidStop在animation结束时设置图层的变换,因为我已经注意到animation只改变了表示层的变换而不是本身的图层。

但是在animation结束时,animation完成时,由于图层在委托函数animationDidStop中更新转换之前返回到其上一个变换,animation中会出现随机闪烁。 我如何消除闪烁?

@implementation ViewController - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag { [CATransaction begin]; [CATransaction setValue: (id) kCFBooleanTrue forKey: kCATransactionDisableActions]; self.parentLayer.transform = CATransform3DRotate(self.parentLayer.transform, DEG2RAD(60.0), 0, 0, 1); [CATransaction commit]; } - (IBAction)rotateBySixtyPressed:(id)sender { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; animation.duration = 3.0; animation.byValue = [NSNumber numberWithFloat:DEG2RAD(60.0)]; [animation setDelegate:self]; [self.parentLayer addAnimation:animation forKey:animation.keyPath]; } 

我已经解决了这个博客: http : //oleb.net/blog/2012/11/prevent-caanimation-snap-back/和这个答案https://stackoverflow.com/a/7690841/3902153

我不再使用委托function。

 - (IBAction)rotateBySixtyPressed:(id)sender { CATransform3D old_transform = self.parentLayer.transform; self.parentLayer.transform = CATransform3DRotate(self.parentLayer.transform, DEG2RAD(60.0), 0, 0, 1); CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"]; animation.fromValue = [NSValue valueWithCATransform3D:old_transform]; animation.duration = 3.0; [self.parentLayer addAnimation:animation forKey:@"transform"]; }