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