Flickering in CABasicAnimation for rotation

771 views Asked by At

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

There are 1 answers

0
user3902153 On

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