1

每当按下按钮时,我都试图将 CAShapeLayer 从其当前角度旋转一个角度。

我正在使用委托函数 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];
}
4

1 回答 1

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"];
}
于 2015-06-23T18:14:54.467 回答