CAKeyFrameAnimation 失败,没有任何线索
下面是我的代码部分,我希望实现不透明度动画以在按钮上显示呼吸效果。
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];
}
}
实际上,我需要知道呼吸动画停止以删除两个 CALayer。但是,我在 animationDidStop()
中得到标志为 NO,它表示动画未正确完成,并且没有显示呼吸动画。 然后,我尝试不调用 [self removeBreathAnimationLayers]
来删除两个 CALayer,动画效果很好。
有人对这个问题有什么建议吗?我真的需要知道动画成功停止才能做其他事情。
感谢您的回复。多谢!
-罗比
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过在动画部分的开头和结尾添加 [CATransaction begin] 和 [CATransaction commit] 来解决此问题。
然后,代码如下:
我是核心动画的新手,希望这能帮助遇到此问题的人。
-罗比
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