CAKeyFrameAnimation 失败,没有任何线索

发布于 2025-01-06 20:38:08 字数 3482 浏览 0 评论 0原文

下面是我的代码部分,我希望实现不透明度动画以在按钮上显示呼吸效果。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

将军与妓 2025-01-13 20:38:08

我通过在动画部分的开头和结尾添加 [CATransaction begin] 和 [CATransaction commit] 来解决此问题。

然后,代码如下:

- (void) breathAnimation {

................
[CATransaction begin];

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

[CATransaction commit];

...................

}

- (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];

}
}

我是核心动画的新手,希望这能帮助遇到此问题的人。

-罗比

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:

- (void) breathAnimation {

................
[CATransaction begin];

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

[CATransaction commit];

...................

}

- (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];

}
}

I'm a newbie on core animation, wish this would help someone who run into this issue.

-Roby

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文