我正在尝试使用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]图层为(空)
2013-09-08 07:12:17.315 Allocator [646:303] Testfield存在且类型为:NSTextField

最佳答案

找到这个SO Answer并意识到我必须为视图设置setWantsLayer:YES,所以我添加了

[[self testTextField] setWantsLayer:YES];


到awakeFromNib并且一切正常

关于cocoa - NSTextField动画,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18675539/

10-13 04:00