Flickering in CABasicAnimation for rotation

懵懂的女人 提交于 2019-12-11 12:51:46

问题


I am trying to rotate a CAShapeLayer by an angle from its current angle whenever a button is pressed.

I am using delegate function animationDidStop to set the transform of the layer during end of animation, since I have noticed animation only changes the transform of the presentation layer and not the layer in itself.

But there are random flickering in the animation which seems to be during the end of the animation when the animation is complete due to layer going back to its previous transform just before the transform is updated in the delegate function animationDidStop. How do I remove the flicker?

@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:


I have solved it referring this blog: http://oleb.net/blog/2012/11/prevent-caanimation-snap-back/ and this answer https://stackoverflow.com/a/7690841/3902153

I am not using the delegate functions any more.

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


来源:https://stackoverflow.com/questions/31009691/flickering-in-cabasicanimation-for-rotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!