使用CAKeyframeAnimation制作图像序列动画

3

我想要对一系列图片进行动画处理,可以使用简单的UIImageView动画来实现;但是,我希望能够检测到动画何时结束,以便允许用户执行其他任务。因此,我尝试使用CAKeyframeAnimation来完成。当我运行以下代码时,程序崩溃了。有人可以告诉我我做错了什么吗?

CALayer *myLayer = [[CALayer alloc]init];
   [myLayer setBounds:CGRectMake(0, 0, 100, 100)];
   [myLayer setPosition:CGPointMake(160.0, 100.0)];
    myLayer.backgroundColor = [[UIColor colorWithRed:4 green:0 blue:0 alpha:1] CGColor];
    [self.window.layer addSublayer:myLayer];

    CAKeyframeAnimation *anim;
    NSArray *images =[NSArray arrayWithObjects:[UIImage imageNamed:@"img1.png"],
                             [UIImage imageNamed:@"img2.png"],nil];  

    anim = [CAKeyframeAnimation animation];
    [anim setKeyPath:@"contents"];
    [anim setValues:images];
    [anim setCalculationMode:@"discrete"];
    [anim setRepeatCount:3];
    [anim setDuration:1.0];
    [myLayer addAnimation:anim forKey:nil];

5
可能是因为您将值设置为 UIImage 而不是 CGImageRef,而 contents 属性需要的是后者,因此才导致崩溃。请检查并更正。 - Patrick Hernandez
1个回答

0

如果像你说的那样可以用简单的UIView动画完成,那么实际上你可以检查是否完成:

[UIView animateWithDuration:duration
                 animations:^(void) {
                     ...
                 }
                 completion:^(BOOL finished) {
                     ...
                 }
 ];

如果由于某些原因你最终无法完成这个任务,那么查看崩溃报告会很有帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接