为什么这个 CAKeyFrameAnimation 会以零不透明度停止?

发布于 2024-07-28 21:59:05 字数 1098 浏览 12 评论 0 原文

我有这个代码。 在值数组的末尾,您可以看到我提供了 0.5 的不透明度。 但由于某种原因,当动画停止时,它会再次闪烁,然后使视图完全透明。 那里出了什么问题?

CALayer *layer = self.layer;
CAKeyframeAnimation *blinkAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
blinkAnim.duration = 1.0;
//blinkAnim.repeatCount = 0;
blinkAnim.autoreverses = NO;

// keyframe times and values
// we want to start fully opaque, fade out, stay faded out and fade back in shortly before the end of the cycle
blinkAnim.keyTimes = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0],
                      [NSNumber numberWithFloat:0.4],
                      [NSNumber numberWithFloat:0.6],
                      [NSNumber numberWithFloat:0.85],
                      [NSNumber numberWithFloat:1.0], nil];
blinkAnim.values = [NSArray arrayWithObjects:   [NSNumber numberWithFloat:1.0],
                    [NSNumber numberWithFloat:1.0],
                    [NSNumber numberWithFloat:0.0],
                    [NSNumber numberWithFloat:0.0],
                    [NSNumber numberWithFloat:0.5], nil];
[layer addAnimation:blinkAnim forKey:nil];

I have this code. At the end of the values array, you can see I provide 0.5 for the opacity. But for some reason, when the animation stops, it flashes once again and then leaves the view completely transparent. What's wrong there?

CALayer *layer = self.layer;
CAKeyframeAnimation *blinkAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
blinkAnim.duration = 1.0;
//blinkAnim.repeatCount = 0;
blinkAnim.autoreverses = NO;

// keyframe times and values
// we want to start fully opaque, fade out, stay faded out and fade back in shortly before the end of the cycle
blinkAnim.keyTimes = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0],
                      [NSNumber numberWithFloat:0.4],
                      [NSNumber numberWithFloat:0.6],
                      [NSNumber numberWithFloat:0.85],
                      [NSNumber numberWithFloat:1.0], nil];
blinkAnim.values = [NSArray arrayWithObjects:   [NSNumber numberWithFloat:1.0],
                    [NSNumber numberWithFloat:1.0],
                    [NSNumber numberWithFloat:0.0],
                    [NSNumber numberWithFloat:0.0],
                    [NSNumber numberWithFloat:0.5], nil];
[layer addAnimation:blinkAnim forKey:nil];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

相思碎 2024-08-04 21:59:05

默认 fillMode (请参阅 此处)是 kCAFillModeRemoved。 您应该将动画的 fillMode 设置为 kCAFillModeForwards,并将 removedOnCompletion 属性设置为 NO(默认情况下)是的):

blinkAnim.removedOnCompletion = NO;
blinkAnim.fillMode = kCAFillModeForwards;

您会注意到这些属性是在我为上一个问题编写的源代码中设置的 - 它们在那里是有原因的。

The default fillMode (see here) for a CAAnimation is kCAFillModeRemoved. You should set the fillMode of your animation to kCAFillModeForwards, and also set the removedOnCompletion property to NO (by default it's YES):

blinkAnim.removedOnCompletion = NO;
blinkAnim.fillMode = kCAFillModeForwards;

You'll notice that those properties were set in the source I wrote for your previous question -- they were there for a reason.

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