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

1 回答 1

0

如果它可以用一个简单的 UIView 动画来完成,如你所说,那么实际上你可以检查完成:

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

如果由于某种原因你不能这样做,那么查看崩溃报告会很有用。

于 2011-11-28T22:24:09.933 回答