SamBo
SamBo

Reputation: 551

Animation is not working after the Home button is pressed

I have a game that has an animated background for the title screen. The animation used is simply a CGMoveToPoint function that moves the image back and forth.

This works perfectly on the simulator, except, when testing on a real device pressing the home button and then re-opening the app, the animation of this image doesn't move.

I was hoping there would be a simple fix. I was thinking something along the lines of a memory issue and using the didReceiveMemoryWarning function, but I am very inexperienced with this or when to use it.

I am targeting iOS 5.1 in my build.

Upvotes: 2

Views: 1136

Answers (3)

Zaartha
Zaartha

Reputation: 1126

Add this to your animation

animation.removedOnCompletion = false;

Upvotes: 0

peterrhodesdev
peterrhodesdev

Reputation: 251

If you didn't want your app to run in the background, then adding the UIApplicationExitsOnSuspend key ("Application does not run in background") to the plist file will work. docs

However, if you do want your app to run in the background, then I suggest using NSNotificationCenter. docs

For example, add the following line to your class's init or viewDidLoad method:

Objective-C

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

Swift

NSNotificationCenter.defaultCenter().addObserver(self, selector: "applicationWillEnterForeground:", name: UIApplicationWillEnterForegroundNotification, object: nil)

Then add the following method to you class:

Objective-C

- (void) applicationWillEnterForeground {
     // add code here to restart animation
}

Swift

func applicationWillEnterForeground(notification: NSNotification) {
    // add code here to restart animation
}

Note, viewWillAppear will not always (if ever) be called when an application enters the foreground after being in the background. So I wouldn't rely on using it to restart the animation.

Upvotes: 0

Paras Joshi
Paras Joshi

Reputation: 20541

Just add the Animation code in viewWillAppear: method instead of viewDidLoad: method..

Upvotes: 1

Related Questions