1

I'm new in ios development. I'm using wheel images in my project.The animation is working fine in foreground mode. After that I pressed the home button.now i relaunch the app the wheel animation is not working. this is my code:

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
animation.duration = 1.0f;
animation.repeatCount = INFINITY;
[imageLeft.layer addAnimation:animation forKey:@"SpinAnimation"];
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
SBM
  • 1,025
  • 9
  • 23
  • 1
    How did you do you animation? Post some code. The question posted now is to vague to gif you a correct answer. – rckoenes Jun 17 '13 at 07:54
  • CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.fromValue = [NSNumber numberWithFloat:0.0f]; animation.toValue = [NSNumber numberWithFloat: 2*M_PI]; animation.duration = 1.0f; animation.repeatCount = INFINITY; [imageLeft.layer addAnimation:animation forKey:@"SpinAnimation"]; – SBM Jun 17 '13 at 07:56
  • You should edit in your question. – rckoenes Jun 17 '13 at 07:58
  • CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.fromValue = [NSNumber numberWithFloat:0.0f]; animation.toValue = [NSNumber numberWithFloat: 2*M_PI]; animation.duration = 1.0f; animation.repeatCount = INFINITY; [imageLeft.layer addAnimation:animation forKey:@"SpinAnimation"]; – SBM Jun 17 '13 at 07:59
  • is there any way to animate the image in background mode. – SBM Jun 17 '13 at 08:04

2 Answers2

3

Try this,

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addAnimation:) name:UIApplicationWillEnterForegroundNotification object:nil];

}

- (void)addAnimation:(NSNotification *)notificaiton
 {
 CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
 animation.fromValue = [NSNumber numberWithFloat: 2*M_PI];
 animation.toValue = [NSNumber numberWithFloat:0.0f];
 animation.duration = 4.0f;
 animation.repeatCount = INFINITY;
 [imageLeft.layer addAnimation:animation forKey:@"SpinAnimation"];
 [imageRight.layer addAnimation:animation forKey:@"SpinAnimation"];
 }
//-----------------------------------------------------------------------------

//How to remove an observer for NSNotification

      [[NSNotificationCenter defaultCenter] removeObserver:self];
Ravindhiran
  • 5,304
  • 9
  • 50
  • 82
0

When your app is suspended (send to the background) all animation and timer are sopped. This is to save battery. When your app is brought to the foreground you must start the animation again your self.

You can us the - (void)applicationDidBecomeActive:(UIApplication *)application in the app delegate for this.

Or listen for the UIApplicationDidBecomeActiveNotification notification in the view controller which holds the animation.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • I'm recording audio.The wheel image is rotating in foreground mode.Now i pressed the home button. The audio recorder recording the audio in background mode.when i relaunch the app the recorder recording the audio.but the wheel animation is not rotating. – SBM Jun 17 '13 at 08:12
  • And this is normal, because as I said in my answer all animation are stopped. You will just need to restart them when you come back to the foreground. Just implement the `- (void)applicationDidBecomeActive:(UIApplication *)application` App delegate. – rckoenes Jun 17 '13 at 08:13
  • what is the code i want to use this app delegate method - (void)applicationDidBecomeActive:(UIApplication *)application. how to restart the animation. – SBM Jun 17 '13 at 08:13
  • You recreate you `CABasicAnimation` and add it to your layer again. – rckoenes Jun 17 '13 at 08:39