I have path stored in an array of CGPoints which I’d like to move an image along. Here’s the general code I have so far:
-(void)movePic:(id)sender{
for(int i = 0; i < self.array.count; i++){
CGPoint location = [[self.array objectAtIndex:i] CGPointValue];
[UIView animateWithDuration:0.1 animations:^{
self.imageView.center = location;
} completion:^(BOOL finished){
}];
}
}
The problem is the for loop runs extremely fast, so you only see the animation on the last points. I’m unsure of how to better design this. Ideally, what could I do to make sure one animation finishes before the other begins? Should I not use a for loop? Thanks
Your code assumes that UIView animations run synchronously in the main thread, which they do not.
You seem to have two options
CAKeyframeAnimationfor animating aCALayeralong any amount of sample points (interpolated between them)UIViewanimation for animating aUIViewalong a series of sample points (interpolated between them)The former would be much more efficient – still I thought I oould show you both options.
CAKeyframeAnimation
UIView Animation