2

我正在尝试使用以下代码制作一个简单的 CAKeyframeAnimation:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    CALayer *rotLayer = [CALayer layer];
    rotLayer.bounds = CGRectMake(0.0, 0.0, 100.0, 50.0);
    rotLayer.anchorPoint = CGPointMake(0.5, 0.5);
    rotLayer.position = CGPointMake(160.0, 200.0);
    rotLayer.backgroundColor = [UIColor redColor].CGColor;

    [self.view.layer addSublayer:rotLayer];

    /*
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    basic.duration = 3.0;
    basic.fromValue = [NSNumber numberWithFloat:0.0];
    basic.toValue = [NSNumber numberWithFloat:2.0];

    [rotLayer addAnimation:basic forKey:@"basic"];
    */

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.duration = 3.0;
    animation.keyTimes = @[ [NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:1.0] ];
    animation.values = @[ [NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:2.0] ];
    animation.timingFunctions = @[ [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear] ];
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;

    [rotLayer addAnimation:animation forKey:@"test"];   
}

如果我使用基本动画而不是关键帧之一,一切都会按预期工作,但如果我在模拟器中运行此代码,它会崩溃并显示以下消息:

 -[CAKeyframeAnimation _copyRenderAnimationForLayer:]: unrecognized selector sent to instance 0x7558870
 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CAKeyframeAnimation _copyRenderAnimationForLayer:]: unrecognized selector sent to instance 0x7558870'

这是一个启用 ARC 的新项目。即使将动画设置为属性也不起作用。QuartzCore 框架也是链接。我错了什么?

4

1 回答 1

2

当我运行此代码时,它工作得很好(一个红色矩形围绕锚点旋转,该锚点位于矩形的中心)。

你确定你正确链接了 QuartzCore 框架吗?你导入<QuartzCore/QuartzCore.h>到你的实现文件中了吗?它看起来像是_copyRenderAnimationForLayer:一种内部的 Apple 方法,所以我不质疑它的存在。但是,如果您没有正确链接或导入文件,您的应用程序将不知道该方法存在,然后抛出您看到的异常。

于 2012-12-21T17:57:42.367 回答