3

I'm trying to animate a sequence of images and this could easily be done with the simple UIImageView animation; however, I want to be able to detect when the animation is finished so I can allow user to perform another task. So I was trying to do it with CAKeyframeAnimation. When I ran the following piece of codes, the program crashed. Can someone tell me what I did wrong?

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];
Dov
  • 15,530
  • 13
  • 76
  • 177
P LP
  • 33
  • 4
  • 5
    This might be crashing due to you setting the values to `UIImage`'s and not `CGImageRef`'s which is what the `contents` property requires. – Patrick Hernandez Nov 28 '11 at 23:07

1 Answers1

0

If it can be done with a simple UIView animation, as you say, then in fact you can check for completion:

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

If for some reason you can't do that after all then it would be useful to see the crash report.

tarmes
  • 15,366
  • 10
  • 53
  • 87