我正在尝试用NSTextField做一个简单的动画
- (void)bounceTimeLabel
{
// Create a key frame animation
CAKeyframeAnimation *bounce =
[CAKeyframeAnimation animationWithKeyPath:@"transform"];
// Create the values it will pass through
CATransform3D forward = CATransform3DMakeScale(1.3, 1.3, 1);
CATransform3D back = CATransform3DMakeScale(0.7, 0.7, 1);
CATransform3D forward2 = CATransform3DMakeScale(1.2, 1.2, 1);
CATransform3D back2 = CATransform3DMakeScale(0.9, 0.9, 1);
[bounce setValues:[NSArray arrayWithObjects:
[NSValue valueWithCATransform3D:CATransform3DIdentity],
[NSValue valueWithCATransform3D:forward],
[NSValue valueWithCATransform3D:back],
[NSValue valueWithCATransform3D:forward2],
[NSValue valueWithCATransform3D:back2],
[NSValue valueWithCATransform3D:CATransform3DIdentity],
nil]];
// Set the duration
[bounce setDuration:0.6];
// Animate the layer
NSLog(@"The layer is %@", [[self testTextField] layer]);
if (![self testTextField]) {
NSLog(@"Textfeild is nil");
} else {
NSLog(@"Testfield exists and is of type: %@", [[self testTextField] class]);
}
[[[self testTextField] layer] addAnimation:bounce
forKey:@"bounceAnimation"];
}
当我运行时,没有任何事情发生,此外,当我将层发送到NSTextField实例时,它根据NSlog语句返回null。此代码来自使用UILabel而不是NSTextField的iOS示例...这可能是问题吗?
我的输出是
2013-09-08 07:12:17.314 Allocator [646:303]图层是(null) 2013-09-08 07:12:17.315 Allocator [646:303] Testfield存在且类型为:NSTextField
答案 0 :(得分:0)
找到了这个SO Answer并意识到我必须为视图设置为WantsLayer:YES,所以我添加了
[[self testTextField] setWantsLayer:YES];
到awakeFromNib并且一切正常