Here is my code section below, I wish implement a animation on opacity to show a breath effect on a button.
NSString* kAnimation = @"animation";
NSString* kBreath = @"breath";
NSString* kHide = @"hide";
NSString* kOpacity = @"opacity";
- (void) breathAnimation {
................
CAKeyframeAnimation *darkblueBreathAnimation = [CAKeyframeAnimation animationWithKeyPath:kOpacity];
NSArray *darkblueOpacityValues = [NSArray arrayWithObjects:[NSNumber numberWithFloat:1.0f],
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:1.0f],
nil];
NSArray *darkblueOpacityTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:0.5f],
[NSNumber numberWithFloat:1.0f],
nil];
[darkblueBreathAnimation setValues:darkblueOpacityValues];
[darkblueBreathAnimation setKeyTimes:darkblueOpacityTimes];
[darkblueBreathAnimation setDuration:1.0f];
[darkblueBreathAnimation setRepeatCount:2];
[darkblueBreathAnimation setFillMode:kCAFillModeRemoved];
[darkblueBreathAnimation setCalculationMode:kCAAnimationLinear];
[darkblueBreathAnimation setRemovedOnCompletion:YES];
[darkblueBreathAnimation setDelegate:self];
[darkBlueLayer_ addAnimation:darkblueBreathAnimation forKey:kBreath];
CAKeyframeAnimation *lightblueBreathAnimation = [CAKeyframeAnimation animationWithKeyPath:kOpacity];
NSArray *lightblueOpacityValues = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:1.0f],
[NSNumber numberWithFloat:0.0f],
nil];
NSArray *lightblueOpacityTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:0.5f],
[NSNumber numberWithFloat:1.0f],
nil];
[lightblueBreathAnimation setValues:lightblueOpacityValues];
[lightblueBreathAnimation setKeyTimes:lightblueOpacityTimes];
[lightblueBreathAnimation setDuration:1.0f];
[lightblueBreathAnimation setRepeatCount:2];
[lightblueBreathAnimation setFillMode:kCAFillModeRemoved];
[lightblueBreathAnimation setCalculationMode:kCAAnimationLinear];
[lightblueBreathAnimation setRemovedOnCompletion:YES];
[lightblueBreathAnimation setDelegate:self];
[lightblueBreathAnimation setValue:kBreath forKey:kAnimation];
[lightBlueLayer_ addAnimation:lightblueBreathAnimation forKey:kBreath];
...................
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
NSString* value = (NSString*)[anim valueForKey:kAnimation];
NSLog(@"Roby: value = %@", value);
if ([[anim valueForKey:kAnimation] isEqualToString:kHide]) {
//TODO
} else if ([[anim valueForKey:kAnimation] isEqualToString:kBreath]) {
[self removeBreathAnimationLayers];
}
}
Actually speaking, I need know the breath animation stop to remove the two CALayer. But, I got flag is NO in animationDidStop(), it indicates animation is not complete properly, and no breath animation shown.
Then, I tried to not invoke [self removeBreathAnimationLayers] to remove the two CALayer, the animation works well.
Does anyone have any suggestion on this issue? I really need know the animation stop successfully to do something else.
Appreciate for your reply. Thanks a lot!
-Roby
I got the way to fix this issue by adding [CATransaction begin] and [CATransaction commit] at the beginning and ending of the animation section.
Then, the code is like below:
I’m a newbie on core animation, wish this would help someone who run into this issue.
-Roby