动画 CATextLayer 的 foregroundColor 属性

发布于 2024-10-19 14:44:33 字数 708 浏览 2 评论 0原文

我想为 iPhone 上 CATextLayer 的文本颜色(前景色)的变化添加动画效果。根据文档,该属性的隐式动画在 OSX 10.6 上不可用,但没有提及 iOS。因此,我假设动画必须是明确的才能在 iOS 上运行。

我使用 +(class)layerClass 更改了支持层类,并在 UIView init 方法中为该属性设置了初始值。文本以及我应用的所有属性(阴影、字体等)显示得很好,但动画没有产生任何影响。

下面是我的动画方法,我从 KVO 观察方法调用该方法,以便视图在观察到的属性发生更改时提醒用户。

-(void) animateTextChange{
    animation = [CABasicAnimation animationWithKeyPath:@"foregroundColor"];

    [animation setFromValue:[[UIColor blackColor] CGColor]];

    [animation setToValue:[[UIColor whiteColor] CGColor]];

    [animation setDuration:2.0f];

    [[self layer] addAnimation:animation
                        forKey:@"foregroundColor"
 ];
}

一如既往地感谢。

I want to animate the change in the color of text(foregroundColor) of a CATextLayer on the iPhone. According to the documentation, implicit animation of the property is not available on OSX 10.6, but no mention is made of iOS. As a result i've just assumed that the animation must be explicit for it to work on iOS.

I've changed the backing layer class using +(class)layerClass, and set an initial value for the property in the UIView init method. The text shows up just fine, along with all of the properties i've applied (shadow, font etc), yet the animation fails to have any impact.

Below is my animation method, that i call from a KVO observation method so that the view alerts the user when the observed property has changed.

-(void) animateTextChange{
    animation = [CABasicAnimation animationWithKeyPath:@"foregroundColor"];

    [animation setFromValue:[[UIColor blackColor] CGColor]];

    [animation setToValue:[[UIColor whiteColor] CGColor]];

    [animation setDuration:2.0f];

    [[self layer] addAnimation:animation
                        forKey:@"foregroundColor"
 ];
}

Thanks as always.

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

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

发布评论

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

评论(1

尝蛊 2024-10-26 14:44:34

这是我成功做到这一点的唯一方法:

       [CATransaction begin];
       [CATransaction setAnimationDuration:.5];
       [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
       [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        yellowWord.foregroundColor =[UIColor redColor].CGColor;
        [CATransaction commit];

希望它能帮助别人

This is the only way i succeeded to do that:

       [CATransaction begin];
       [CATransaction setAnimationDuration:.5];
       [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
       [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        yellowWord.foregroundColor =[UIColor redColor].CGColor;
        [CATransaction commit];

Hope it will help someone

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