macos 如何使用核心动画为 NSTextField 的背景颜色设置动画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4642949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:35:06  来源:igfitidea点击:

How can I use core animation to animate the background color of an NSTextField?

objective-ccocoamacoscore-animation

提问by Reid Beels

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

我正在尝试使用核心动画来突出显示无效的文本字段。

[[my_field animator] setBackgroundColor [NSColor yellowColor]]

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.

更新字段背景颜色,但不为更改设置动画。更新属性(例如字段位置)会正确设置动画。我假设这是因为 NSAnimatablePropertyContainer 搜索中不包含背景颜色。

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"];

Suggestions for accomplishing this?

实现这一目标的建议?

采纳答案by Reid Beels

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.

虽然我从未设法弄清楚如何为背景颜色设置动画,但我能够通过为 CIFalseColor 过滤器设置动画来创建所需的效果。

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"];

回答by Arvin

Merry Christmas:

圣诞节快乐:

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"];

This will animate the background color of a view using a backed layer. Remember to set wants layer in the init or awake:

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

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