Search code examples
iphoneioscore-graphicsuiviewanimationcgaffinetransform

Problems with UIView animation


I am performing animation on my view. But I am facing few problems with that. I have to button's startAnimation and Stop Animation. For performing animation on my imageview I am using following code in startAnimation button's action:

 UIViewAnimationOptions option = UIViewAnimationOptionRepeat + UIViewAnimationOptionAutoreverse;
[UIView animateWithDuration:2.5 delay:0.0 options:option
                 animations:^{
                     CGAffineTransform scale = CGAffineTransformMakeScale(2.0, 2.0);
                     CGAffineTransform translate = CGAffineTransformMakeTranslation(-50.0, -100.0);
                     CGAffineTransform mix = CGAffineTransformConcat(scale, translate);
                     imv.transform = mix; 
                 } 
                 completion:^(BOOL finished) {

                 }
 ];

This is working. Now the problems I am facing are:

  1. How to stop this animation on my stopAnimation button's action?
  2. If ImageView is animation and I enter in background after that I again come in foreground ImageView sticks there and do not animates.

How to solve these issues?


Solution

  • On your stopAnimation button you would need to remove animations from the UIView's layer. First you must import Quartzcore.framework.

    #import <QuartzCore/QuartzCore.h>
    
    
    [yourView.layer removeAllAnimations];
    

    As for your other issue of pausing your animation while going into the background and coming back to foreground, theres no way to 'pause' an animation. Your best bet would be to manually handle that situation by restarting your animation.