在两个 CAGradientLayer 颜色设置之间连续淡入淡出?

发布于 2024-11-10 07:23:43 字数 113 浏览 0 评论 0原文

我想要一个 UIButton 发出脉冲,并且我计划通过在 CAGradientLayer 上的两个颜色数组之间缓慢淡入淡出来实现此目的。有没有办法来回重复动画?

I want a UIButton to pulse, and I plan to do this by fading slowly between two color arrays on a CAGradientLayer. Is there a way to repeat an animation back and forth?

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

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

发布评论

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

评论(1

挽容 2024-11-17 07:23:43

是的。在核心动画中,您创建一个显式动画来执行淡入淡出,然后还必须为动画对象设置其他两个属性:autoReversesrepeatCount(要执行的重复次数,每重复 2 次将带您浏览动画并再次返回)。在您的情况下,您将以下行添加到设置动画对象的代码中(我将调用该对象 anim):

 anim.repeatCount = HUGE_VALF;
 anim.autoReverses = YES;

HUGE_VALF 导致动画永远重复,尽管您可以指定更大的数字比可能发生的任何重复次数都要多。

这些属性未显示在 CAAnimation 对象或其子类的文档中,因为它是在 CAAnimation 及其子类采用的 CAMediaTiming 协议中定义的。但是,您可以在动画类型和计时编程指南计时、时空和CAAnimation部分中查看适用于CAAnimation对象的CAMediaTiming协议的示例和讨论Apple 的开发者网站或通过 XCode 提供的文档中。

(许多人似乎发现苹果的核心动画文档特别难以理解,除非你对不同的部分有一个很好的整体掌握。我基本上知道你必须做什么,但仍然发现很难准确记住在哪里可以找到实际信息至于所涉及的属性。)

Yes. In Core Animation you create an explicit animation to do the fade and then you must also set two other properties for the animation object: autoReverses and repeatCount (number of repetitions to perform, each 2 repetitions will take you through your animation and back again). In your case you’d add the following lines to your code that sets up the animation object (I’ll call the object anim):

 anim.repeatCount = HUGE_VALF;
 anim.autoReverses = YES;

HUGE_VALF causes the animation to repeat forever though you could specify a number larger than any amount of repetitions that might occur.

These properties aren’t shown in the documentation of the CAAnimation object or it’s subclasses since it is defined in the CAMediaTiming Protocol which is adopted by CAAnimation and it's subclasses. But you can see examples and discussion of the CAMediaTiming protocol as it applies to CAAnimation objects in the Timing, Timespaces, and CAAnimation section of the Animation Types and Timing Programming Guide either on Apple’s Developer site or in the documentation provided through XCode.

(Many people seem to find Apple's Core Animation documentation to be particularly hard to understand until you get a good overall grasp of the disparate parts. I basically knew what you had to do but still found it hard to remember exactly where to find the actual information as to the properties involved.)

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