微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – 如何使用核心动画来动画化NSTextField的背景颜色?

我试图使用核心动画来突出显示一个文本字段是无效的.
[[my_field animator] setBackgroundColor [NSColor yellowColor]]

更新字段背景颜色,但不会更改动画.更新属性,如字段的位置适当地动画.我假设这是因为NSAnimatablePropertyContainer搜索中没有包含背景颜色.

我也试图明确地创建动画,无济于事.

CABasicAnimation *ani;
ani = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];

ani.fromValue = CGColorCreateGenericRGB(1.0,1.0,1.0);
ani.tovalue = CGColorCreateGenericRGB(1.0,0.0,1.0);
ani.repeatCount = 2;
ani.autoreverses = YES;
ani.duration = 1.0;

[[my_field layer] addAnimation:ani forKey:"backgroundColor"];

完成这个建议?

解决方法

圣诞节快乐:
NSView *content = [[self window] contentView];
CALayer *layer = [content layer];

CABasicAnimation *anime = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
anime.fromValue = (id)[layer backgroundColor];
anime.tovalue = (id)CGColorCreateGenericGray(0.0f,1.0f);
anime.duration = 5.0f;
anime.autoreverses = YES;

[layer addAnimation:anime forKey:@"backgroundColor"];

这将使用支持的图层动画化视图的背景颜色.记住要在init或awake中设置需求层:

[[[self window] contentView] setWantsLayer:YES];

原文地址:https://www.jb51.cc/c/113921.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐