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。但是,我得到 flag is NO in animationDidStop(),这表明动画没有正确完成,并且没有显示呼吸动画。然后,我尝试不调用[self removeBreathAnimationLayers]删除两个CALayer,动画效果很好。

有人对这个问题有什么建议吗?我真的需要知道动画成功停止才能做其他事情。

感谢您的回复。非常感谢!

-罗比

4

1 回答 1

0

我通过在动画部分的开头和结尾添加 [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];

}
}

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

-罗比

于 2012-02-21T02:06:15.413 回答