2

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?

Kapil Choubisa
  • 5,152
  • 9
  • 65
  • 100

3 Answers3

5

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.

skram
  • 5,314
  • 1
  • 22
  • 26
  • The problem is restarting of annimation doesn't work.. until I remove my imageview from superview and add it back – Kapil Choubisa May 23 '12 at 07:24
  • For restarting your animation you can do the following yourview.transform = CGAffineTransformIdentity; After this if you restart your animation it will work – user1988 Sep 03 '15 at 14:21
2

To stop the animation you can use

#import <QuartzCore/QuartzCore.h>
...

[imv.layer removeAllAnimations];

As for making an animation restart it is a complex problem and the answer depends on what you want the application to do when it restores. One possible answer would be to keep track of where you are animating the ImageView to and then in ApplicationDidEnterBackground you could cancel all the animations and simply move it there. Then when you restored it would be in the correct location.

If you actually want to continue the animations I would have a read of some other Stack Overflow answers and see if that helps.

Restoring animation where it left off when app resumes from background

iOS, Restarting animation when coming out of the background

Community
  • 1
  • 1
Ben Trengrove
  • 8,191
  • 3
  • 40
  • 58
0

For further reference, for the second part of your response you have to restore the matrix to the identity before doing anything:

imv.transform = CGAffineTransformIdentity;
Petrescu Silviu
  • 247
  • 1
  • 11