Pause game on orientation change, objective-c / Sprite Kit - objective-c

I want my game to pause when device orientation is initiated. I have this method in my viewcontroller, which works fine:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(#"I am starting to rotate, should pause game..");
}
But how to listen for device rotation from within my SKScene, where the actual game is playing. Hope I have made myself clear enough. Thanks for any help!

You can use NSNotification. Keeping with your code, add the following line in your ViewController init:
[[NSNotificationCenter defaultCenter]
postNotificationName:#"RotateNotification"
object:self];
Then in your SKScene init add this line:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveNotification:)
name:#"RotateNotification"
object:nil];
Also add the called method to SKScene:
-(void) receiveNotification:(NSNotification *)notification
{
NSLog (#"Received notification");
// do what you have to do here...
}

Related

UIDevice's beginGeneratingDeviceOrientationNotifications doesn't work well

I have a subview in a subclass of UIView. When the device is rotated I want the subview to remove. This is my code:
- (void)awakeFromNib
{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged
{
[subview removeFromSuperview];
}
My problem is that even if the device is tilted a little bit, or put on for example a table the subview removes. What can I do to prevent this?
Don't track the device orientation. Track the interface orientation. Use UIApplicationWillChangeStatusBarOrientationNotification instead of UIDeviceOrientationDidChangeNotification, and get the new interface orientation out of the notification's userInfo. (You won't have to do beginGeneratingDeviceOrientationNotifications if you track interface orientation.)

Handle device rotation for "More" UINavigationController in UITabBarController.moreNavigationController

I have an app which has to work in both portrait and landscape more and the UITabBar should adjust to current orientation (it has custom background and selected items). So, for the rest of views I just override the - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation method and it works perfectly.
How would I do that for the .moreNavigationController of UITabBarController ? I've tried adding an observer (the selector is in extension of UITabBarController):
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:self.moreNavigationController];
but it never get called.
Am I missing something or what would be the best way to handle this situation ?
Solution: somewhy UIDeviceOrientation is not firing correctly, so better to use statusBarOrientation, works as a charm.
the final code which work is this:
in main UITabBarController, viewDidLoad:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
the didRotate selector method:
- (void) didRotate:(NSNotification *)notification{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(orientation)) {
// Portrait
} else {
// Landscape
}
}
Thanks for help.
You are registering your UITabBarController for a notification which never gets posted. Take a look at the documentation NSNotificationCenter Reference for the addObserver:selector:name:object method
- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender
notificationSender:
The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer.
If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.
so, if you specify the . moreNavigationController as the sender, you wont get those notifications, because it never posts such ones. Instead pass nil to ignore the sender and listen to the status bar change regardless of who sent it.
By the way, in this SO Answer is a summary of how you can react to orientation change.
And at last. If it still doesn't work, you can try this:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
Yes, you forgot to post notification, which will call you own notification:
[[NSNotificationCenter defaultCenter] postNotificationName: UIApplicationDidChangeStatusBarOrientationNotification object:self];
or if you dont wont to send anything just set object as nil:
[[NSNotificationCenter defaultCenter] postNotificationName: UIApplicationDidChangeStatusBarOrientationNotification object:nil];
The best way to implement the same is first addObserver and then remove observer to avoid the crash:-
-(void)viewDidLoad{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
//Now Remove Observer
-(void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self #"UIDeviceOrientationDidChangeNotification" object:nil];
}

youtube full screen video not calling viewWillAppear or viewWillRotate

So I have a UIWebView in which sometimes it has a youtube embedded player in it. When I play it and go to full screen and rotate and then dismiss the video, it doesn't call the viewWillAppear or the willRotateTo.... why is this?? I need to make some view adjustments when the device is rotated, however when the player is presented, for some reason none of these methods are being called. And yes I have shouldAutoRotateToInterfaceOrientation set correctly. Any idea?
You can use following for your UIwebView problem, viewWillAppear or willRotateTo.. never calls on UIWebView. You can detect the end of full screen mode by observing the #"UIMoviePlayerControllerDidExitFullscreenNotification" mode:
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidExitFullscreen:)
name:#"UIMoviePlayerControllerDidExitFullscreenNotification"
object:nil];
}
- (void)viewDidUnload
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)moviePlayerDidExitFullScreen:(NSNotification *)notification
{
// This is where you do whatever you want.
}
Your controller must be a UIViewController or the viewWillAppear delegate won't be called.

How to close a modal view when the application enter in background on ios

I have a modal view created in a method (there is no reference in the mainview) and I want to do a dismissModalViewControllerAnimated automatically when my app enter in background. How can I do that ?
In the mainview's viewDidLoad, add observer to be notified when app goes to background.
- (void) viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(goToBackground)
name:UIApplicationWillResignActiveNotification object:nil];
}
Define the function goToBackground(). It will be called when the app goes to background
- (void) goToBackground
{
[self dismissModalViewControllerAnimated: NO]; // no need to animate
}
Don't forget to remove the observer
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
You can use a notification. Post a notification from the ApplicationDelegate's method applicationDidEnterBackground:. YOu can call the dismiss method from the modal controller, so add it as observer to the notification center.

Removing a NSNotificationCenter observer in iOS 5 ARC

I have an iOS 5 ARC-based project, and am having difficulty about where I should be removing the observer for the NSNotificationCenter observations which I have registered within a UIViewController. Similar posts on SO have said this should be done in the -dealloc method. Even though this method is not required in ARC projects I have added it with the following code:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
As a test, I open the UIViewController (within a UINavigationController), do some things which trigger the notifications, and then pop it off the stack by tapping the Back button. I then reopen the UIViewController, and do some more things to trigger the notifications, but notice that each callback is being called twice - an indication that the previous notifications have not been deregistered. Repeating this procedure just causes each callback to be called more than more times, so they appear to never be deregistering.
Any help would be appreciated!
It's pretty clear your dealloc method isn't being called (nor is the removeObserver call).
Why not remove your UIViewController's observer in the viewDidUnload: or viewWillDisappear: methods?
If your dealloc isn't being called, it's likely because someone is still holding a reference to the view controller. Perhaps you need to mark something as __weak? You can use the allocations instrument to help track down what's holding on to your view controller.
"I also need the notification callbacks to still be fired if the view is off-screen" -> you may need to register UIApplicationWillEnterForegroundNotification. If so, let try this:
- (void)viewWillAppear:(BOOL)animated {
NSLog(#"viewWillAppear");
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
NSLog(#"viewWillDisappear");
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(#"applicationWillEnterForeground");
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
// do your stuff here
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(#"applicationDidEnterBackground");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
The idea is adding or removing UIApplicationDidEnterBackgroundNotification whenever coming in and out of your screen. We just register UIApplicationWillEnterForegroundNotification when the app enter background and remove once it's back. Be noticed that we just remove UIApplicationDidEnterBackgroundNotification when viewWillDisappear.
My dealloc() is not called by somehow, so I found this way, hope it useful for you too.
Enjoy :)