Determine the end of one animation cycle
This was a slightly tricky one to search for. Not too sure what to bang
into Google or SO, so I apologise if this has been answered before.
So I have two animations that I apply to a CALayer with a duration of 5
seconds (although this is not relevant) and they repeat indefinitely. I
want to be able to gracefully remove these animations on user interaction.
Detecting the interaction is easy but being able to determine when the
animations have reached the end of one cycle is not so easy. By detecting
this I am hoping to achieve the effect of the animation finishing off it's
last cycle and stopping, instead of harshly removing it from the screen
which just look plain unfriendly.
This is what I am doing now and it is not working
- (void)attachFadeAnimation {
// Create a fade animation that compliments the scale such that
// the layer will become totally transparent 1/5 of the way
// through the animation.
CAKeyframeAnimation *fadeAnimation = [CAKeyframeAnimation
animationWithKeyPath:@"opacity"];
fadeAnimation.values = @[@0.8, @0, @0];
[self addAnimation:fadeAnimation withKeyPath:@"opacity"];
}
- (void)addAnimation:(CAKeyframeAnimation *)animation
withKeyPath:(NSString *)keyPath {
// These are all shared values of the animations and therefore
// make more sense to be added here. Any changes here will
// change each animation.
animation.keyTimes = @[@0, @0.2, @1];
animation.repeatCount = HUGE_VALF;
animation.duration = 5.0f;
animation.delegate = self;
[self.layer addAnimation:animation forKey:keyPath];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
if ( !self.emanating )
[self.layer removeAllAnimations];
}
The delegate call to animationDidStop:finished is not called when I was
expecting it to. Clearly I have misunderstood the documentation.
No comments:
Post a Comment