Search code examples
iphoneipadcore-animation

beginAnimation with For Statement not working


-updated code-

-(void)animateDot {
    dotMotion.center = (*doodlePoints)[0];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationRepeatCount:100];
    for(int i = 1; i < doodlePoints->size(); i++){
        dotMotion.center = (*doodlePoints)[i];
    }

    [UIView commitAnimations];
}

I have a vector of points and I want this to do the animation from point to point. I am only getting from the 2nd to the last to the last working. not anything before. Any ideas?

So i tried doing this differently. Nothing. My application has to work 3.x so i can't use animation blocks.


Solution

  • Even better try the following:

    -(void) nextAnimation
    {
        CGPoint point = [[self.animatePoints objectAtIndex:0] CGPointValue];
        [self.animatePoints removeObjectAtIndex:0];
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationRepeatCount:100];
        dotMotion.center = point;
        [UIView commitAnimations];
    }
    
    - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
    {
        if ([self.animatePoints count] > 0) {
            [self nextAnimation];
        }
    }
    
    -(void)animateDot {
        self.animatePoints = [NSMutableArray array];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
        for(int i = 1; i < doodlePoints->size(); i++){
            [self.animatePoints addObject:[NSValue valueWithCGPoint:(*doodlePoints)[i]]];
        }
        [self nextAnimation];
    }
    

    This is a rough example of loading the individual points in an NSMutableArray property that is incrementally processed and emptied as each animation completes. You could just as easily use an internal index property to the doodlePoints array but I did it this way should you ever need to pass in a different collection of points. In my example we set the animation delegate and callback selector so we can be told about each animation completing. We then schedule the an animation to the next point and remove it from the array.