1

I am trying to use following code to perform few animations

-(void) performSlidingfromX:(int) xx fromY:(int) yy 
{
UIImageView *Image= [self getImage];

[UIView beginAnimations:nil context:NULL];  
[UIView setAnimationDuration: 1.0];
[UIView setAnimationBeginsFromCurrentState:true];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[token setFrame:CGRectMake(xx, yy, 64, 64)];
[UIView commitAnimations];

}

and i am calling like it in for loop

for (i = 0; i < totMoves; i++) {
    Moment *m = [moments objectAtIndex:i];
    int xx= [m X];
    int yy= [m Y];

    [self performSlidingfromX:xx fromY:yy];

}

The problem that i am facing is that its animating to final position, for example , If i input the following moments for xx,yy

0,0
50,0
50,50

It moves the image from 0,0 to 50,50 diagonally, I want it to slide to horizantly first and then vertical.

Any Help?

Thanks

nomi
  • 431
  • 1
  • 5
  • 12

3 Answers3

9

use new block animations. it is easy and stable:

[UIView animateWithDuration:0.5 
                          delay:0 
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         [token setFrame:CGRectMake(xx, 0, 64, 64)];
                         //here you may add any othe actions, but notice, that ALL of them will do in SINGLE step. so, we setting ONLY xx coordinate to move it horizantly first.
                     } 
                     completion:^(BOOL finished){

                         //here any actions, thet must be done AFTER 1st animation is finished. If you whant to loop animations, call your function here.
                         [UIView animateWithDuration:0.5 
                                               delay:0 
                                             options:UIViewAnimationOptionBeginFromCurrentState 
                                          animations:^{[token setFrame:CGRectMake(xx, yy, 64, 64)];} // adding yy coordinate to move it verticaly} 
                                          completion:nil];
                     }];
SentineL
  • 4,682
  • 5
  • 19
  • 38
  • hey, number of movements can be dynamic and will be different, Consider following, user can touch the device at different random positions and once he is done, i need to slide the image to all positions he touched. This is just one scenario to give idea about problem. – nomi Nov 02 '11 at 08:25
  • just put your coordinates in array. In "animations" move your picture to 1st coordinates in array and remove them from it. in "completion" call this function (in your case - performSlidingfromX) if any data left in array. It will move your picture to next coordinates, removes them from array, and so on – SentineL Nov 02 '11 at 08:46
  • if i would in your place, i would use NSMutableArray for this. methods "addObject" and "removeObjectAtIndex" will do a lot of work for you – SentineL Nov 02 '11 at 08:49
1

The problem is u continuously calling "performSlidingfromX:xx fromY:yy" inside for loop. Try this code:

     i=0;
     Moment *m = [moments objectAtIndex:i];
     int xx= [m X];
     int yy= [m Y];
     [self performSlidingfromX:xx fromY:yy];

-(void) performSlidingfromX:(int) xx fromY:(int) yy 
{
i++;
[UIView beginAnimations:nil context:NULL];  
[UIView setAnimationDuration: 1.0];
[UIView setAnimationBeginsFromCurrentState:true];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[token setFrame:CGRectMake(xx, yy, 64, 64)];
[UIView commitAnimations];


[self performSelector:@selector(call:) withObject:[NSNumber numberWithInt:i] afterDelay:1.1];

}
-(void)call
{
 Moment *m = [moments objectAtIndex:i];
     int xx= [m X];
     int yy= [m Y];
     [self performSlidingfromX:xx fromY:yy];
 }
banu
  • 787
  • 9
  • 24
  • Good except i is not defined in call. You can fix it by giving call an NSNumber parameter and incrementing it each time. (Needs to be NSNumber, not int, so you can use it in the withObject parameter of performSelector.) – morningstar Nov 02 '11 at 08:54
  • hey banu,sorry for getting really late on this, never the les I tried your solution, the problem that i am gettng now is that moment of its not 1 continious moment, so basically, it animates through all position, but it starts and stops for every position. how can i make it continious? – nomi Nov 13 '11 at 12:23
0

Making an animation is not a blocking call. Your code does not stop and wait for the animation to finish. Immediately after you start the animation, the next iteration of your loop will run. That creates a new animation, which affects the same property, so it replaces the previous animation. The result you get is as if you had only run the last iteration of your loop.

Unfortunately there is no simple block of code to do what you want to do. You need to detect when the animation finishes, and then start the next one. You need to keep track of your state (mostly which i you are on) in something with wider scope than local variables.

morningstar
  • 8,952
  • 6
  • 31
  • 42