1

我想旋转CAShapeLayer从当前角度角度每当按钮被按下。闪烁CABasicAnimation旋转

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

但动画中存在随机闪烁,在动画完成时,似乎在动画结束时,由于图层在委托函数animationDidStop中更新变换之前返回到其先前变换。我如何消除闪烁?

@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]; 
} 

回答

1

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

我没有使用委托功能了。

- (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"]; 
}