如何使用核心动画来动画化NSTextField的背景颜色? [英] How can I use core animation to animate the background color of an NSTextField?

查看:186
本文介绍了如何使用核心动画来动画化NSTextField的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用核心动画将文本字段突出显示为无效。

I'm trying to use core animation to highlight a text field as being invalid.

[[my_field animator] setBackgroundColor [NSColor yellowColor]]

更新字段背景颜色,但不会对更改进行动画处理。更新属性,如字段的位置正确动画。我假设这是因为背景颜色不包括在NSAnimatablePropertyContainer搜索中。

Updates the field background color, but does not animate the change. Updating properties such as the position of the field animates properly. I'm assuming this is because background color isn't included in the NSAnimatablePropertyContainer search.

我也试图显式地创建动画,没有效果。 >

I've also tried creating the animation explicitly, to no avail.

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

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

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

要完成此建议吗?

推荐答案

虽然我从来没有想出如何动画的背景颜色,我能够通过动画CIFalseColor过滤器创建所需的效果。

While I never managed to figure out how to animate the background color, I was able to create the desired effect by animating a CIFalseColor filter.

CIFilter *filter = [CIFilter filterWithName:@"CIFalseColor"];
[filter setDefaults];
[filter setValue:[CIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0] forKey:"inputColor0"];
[filter setValue:[CIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0] forKey:"inputColor1"];
[filter setName:@"pulseFilter"];
[[myField layer] setFilters:[NSArray arrayWithObject:filter]];

CABasicAnimation* pulseAnimation = [CABasicAnimation animation];
pulseAnimation.keyPath = @"filters.pulseFilter.inputColor1";

pulseAnimation.fromValue = [CIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
pulseAnimation.toValue = [CIColor colorWithRed:0.995 green:1.0 blue:0.655 alpha:1.0];

pulseAnimation.duration = 0.3;
pulseAnimation.repeatCount = 1;
pulseAnimation.autoreverses = YES;

[[myField layer] addAnimation:pulseAnimation forKey:@"pulseAnimation"];

这篇关于如何使用核心动画来动画化NSTextField的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆