I am drawing a path in my UIView's drawRect method. In certain situations that path will go away and I want to animate this - let's call it "undrawing" of the path.
For the animation, I am creating a CAShapeLayer, give it myPath and then set up a CABasicAnimation (animationWithKeyPath:@"strokeEnd").
My problem is how to switch from what is drawn in drawRect to the animation. I have to remove myPath from what is drawn from drawRect and call setNeedsDisplay - otherwise the animation would be hidden by the path drawn from drawRect.
- (void)animationDidStart:(CAAnimation *)theAnimation
{
myPath = nil;
[self setNeedsDisplay];
}
But this way I see my path, then a quick flickering with no path, then the path is rendered by CoreAnimation again and is nicely undrawn.
Can I do better to avoid the flickering?
Background This is how I set up the animation:
- (void) startAnimation
{
pathLayer.path = myPath.CGPath;
pathLayer.hidden = false;
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 1;
pathAnimation.fromValue = [NSNumber numberWithFloat:1.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.delegate = self;
[pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
}
kCAFillModeBoth
fix the flicker for you? – David Rönnqvist