2011-01-10 74 views
9

我正在嘗試使用核心動畫來突出顯示文本字段爲無效。如何使用核心動畫爲NSTextField的背景顏色設置動畫效果?

[[my_field animator] setBackgroundColor [NSColor yellowColor]] 

更新字段背景顏色,但不更改動畫。更新屬性,如場的位置動畫適當。我假設這是因爲背景顏色不包括在NSAnimatablePropertyContainer搜索。

我也嘗試明確創建動畫,無濟於事。

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

完成此操作的建議?

回答

5

雖然我從未弄清楚如何爲背景色設置動畫,但我可以通過動畫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"]; 
+0

公共10.11(El Capitan)測試版不支持應用自定義過濾器(具有自定義名稱的過濾器)。但是,如果您省略setName行並將keypath設置爲@「filters。CIFalseColor.inputColor1」,代碼可以正常工作。直。 – deflozorngott 2015-07-21 17:18:40

15

聖誕:

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或喚醒中設置想要的圖層:

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