0

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

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
John Riselvato
  • 12,854
  • 5
  • 62
  • 89

3 Answers3

1

You need to exit from your for loop, and return from this method, after each line segment for each animation segment to be displayed. The UI is only updated after you return to the UI run loop.

Continue each iteration of the loop in another delayed or delegate callback method. Save the iterrator variable in an instance variable between methods.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • i dont understand how to get out of the loop. and go back in. – John Riselvato Nov 10 '11 at 01:55
  • 1
    Getting out is easy. Just add a "return" statement. Understanding how to get back in requires figuring out how loops and methods work, as well as state, and variables, and scope, and run loops, and delayed operations and/or NSTimers. Too much for 1 comment. – hotpaw2 Nov 10 '11 at 02:08
  • its a void so i guess i `break;`? – John Riselvato Nov 11 '11 at 19:11
1

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.

Cliff
  • 10,586
  • 7
  • 61
  • 102
  • I really appreciate your help and the time your taking out to help me with this. I wont be able to test this out until monday unfortunately but thank you dearly. – John Riselvato Nov 12 '11 at 19:54
  • what is animatePoints? Each line that has animatePoints has an issue. What should i declare it as? – John Riselvato Nov 14 '11 at 16:18
  • Alright I read about NSMutableArray and learned a little. I understand that animatePoints has to be an id. I am trying to figure out how to set it up as an id in my header file. I'll report back if i figure it out. – John Riselvato Nov 14 '11 at 16:43
  • 1
    My apologies, animatePoints is simply an NSMutableArray declared as a property in your interface: @property (nonatomic, retain) NSMutableArray *animatePoints; – Cliff Nov 14 '11 at 19:32
-1

You want something similar to this (not tested):

-(void)animateTo:(CGPoint) point {
    dotMotion.center = (*doodlePoints)[0];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationRepeatCount:100];
    dotMotion.center = point;
    [UIView commitAnimations];
}

-(void)animateDot {
    for(int i = 1; i < doodlePoints->size(); i++){
        [self animateTo:(*doodlePoints)[i]];
    }
}

Though you probably don't want to schedule these to run concurrently. You'd probably like to chain the animations using the animation completion callback and fire off the next one in sequence after each animation completes. Edited for formatting

Cliff
  • 10,586
  • 7
  • 61
  • 102
  • `-(void)animateTo:(int) point {` should be (CGPoint) but how were you to know. Thanks I'll keep messing with this. The point just stays still at the start from what I am seeing. and thats because the doodlePoints->begin() and doodlePoints->end() are touching. So its not doing any animation. – John Riselvato Nov 11 '11 at 19:53
  • Just tested, its going from begin() to end() for the doodlePoints instead of all the points. Should i be using doodlePoints->size()? – John Riselvato Nov 11 '11 at 19:58