0

I have an UIImageView with the image of a face, then I apply a rotation transform animation to that UIImageView.

 self.transform = CGAffineTransformRotate(self.transform, M_PI / 16);

[UIView animateWithDuration:0.5
                  delay:0.0
                options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse
             animations:^{

                 self.transform = CGAffineTransformRotate(self.transform, -1 * (M_PI/8));

             }
             completion:^(BOOL completed) {}];

Then I have another animation for the same face image that moves up and down on y axis.

[UIView animateWithDuration:0.2
                      delay:0.0
                    options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse
                 animations:^{

                     self.transform = CGAffineTransformMakeTranslation(0, 10);
                 }
                 completion:^(BOOL completed){}];

After I execute the first animation I need to change UIImageView position and then start the second animation.

I have a stopAnimation method that remove animations, reset the transform, and set the position of the UIImageView.

-(void)stopAnimation:(NSInteger)currentAnimation{

    [self.layer removeAllAnimations];

    [self setTransform:CGAffineTransformIdentity];
    [self setFrame:initFrame];

    CGPoint position = [self getNextPosition:currentAnimation];
    //[self setTransform:CGAffineTransformMakeTranslation(0, 0)];
    CGRect frame = self.frame;
    frame.origin.x = position.x;
    frame.origin.y = position.y;
    self.frame = frame;
}

-(CGPoint)getNextPosition:(NSInteger)currentAnimation{

    CGPoint position = [[positions objectAtIndex:(currentAnimation-1)] CGPointValue];
    NSLog(@"Current animation:%u %f:%f", currentAnimation,position.x,position.y);
    return position;
}

If I execute the change of position without the animations the UIImageView are in the correct position of the screen but if I run with the animation, the animations work good but the UIImageView position is wrong.

I tried the same setting the center of the UIImageView but I have same problem.

Tony
  • 287
  • 5
  • 19

1 Answers1

0

Try offsetting the second animation by the duration of the first animation.

[UIView animateWithDuration:0.2
                      delay:0.51 //delay by the duration of the first animation
                    options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse
                 animations:^{
                     self.transform = CGAffineTransformConcat(self.transform, CGAffineTransformMakeTranslation(0, 10));
                 }
                 completion:^(BOOL completed){}];

You could also try putting the second animation block in the completion block of the first animation.

architectpianist
  • 2,562
  • 1
  • 19
  • 27
  • But, I have the control of the time when the animations runs, also after execute an animation I removeAllAnimations. – Tony Mar 12 '13 at 18:45
  • Where does stopAnimation get called? Some general notes since I don't know how you have it worked: The UIView animations are asynchronous, so your code below the animation block will get called before the animation finishes. Also, setting the transform to a translation will remove the rotation. – architectpianist Mar 12 '13 at 18:55
  • The app start with the first animation, then when the user tap stopAnimation is called, and after that the second animation is called. – Tony Mar 12 '13 at 21:25
  • Check out this [post](http://stackoverflow.com/a/8719887/2152503) which says that calling [self.layer removeAllAnimations] may not take effect immediately in iOS 5+. Also see the other answers, namely UIViewAnimationOptionBeginFromCurrentState. I apparently misunderstood your code, sorry. :) – architectpianist Mar 12 '13 at 22:51