Reputation: 551
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
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
Reputation: 20541
Just add the Animation code in viewWillAppear:
method instead of viewDidLoad:
method..
Upvotes: 1