2011-01-10 39 views
9

Tôi đang cố gắng sử dụng hoạt ảnh lõi để đánh dấu trường văn bản là không hợp lệ.Làm cách nào để sử dụng hoạt ảnh lõi để tạo hiệu ứng cho màu nền của một NSTextField?

[[my_field animator] setBackgroundColor [NSColor yellowColor]] 

Cập nhật màu nền của trường nhưng không tạo hiệu ứng thay đổi. Cập nhật các thuộc tính như vị trí của trường hoạt ảnh chính xác. Tôi giả định điều này là vì màu nền không được bao gồm trong tìm kiếm NSAnimatablePropertyContainer.

Tôi cũng đã cố gắng tạo hoạt ảnh một cách rõ ràng, không có kết quả.

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

Gợi ý để hoàn thành điều này?

Trả lời

5

Trong khi tôi chưa bao giờ tìm ra cách tạo hiệu ứng nền màu, tôi có thể tạo hiệu ứng mong muốn bằng cách tạo bộ lọc 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

Có vẻ như áp dụng bộ lọc tùy chỉnh (bộ lọc có tên tùy chỉnh) không được bản beta 10.11 công khai (El Capitan) hỗ trợ. Tuy nhiên, mã này hoạt động, khi bạn bỏ qua dòng setName và đặt đường dẫn khóa thành bộ lọc @ ". CIFalseColor.inputColor1"; trực tiếp. – deflozorngott

+1

Cũng kể từ 10.9 bạn cần thêm self.layerUsesCoreImageFilter = YES; – deflozorngott

15

Giáng sinh vui vẻ:

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

này sẽ biến đổi màu nền của một cái nhìn sử dụng một lớp hậu thuẫn. Hãy nhớ đặt lớp trong init hoặc awake:

[[[self window] contentView] setWantsLayer:YES]; 
Các vấn đề liên quan