Can we change InterfaceOrientations from SKScene in spritekit? - ios7

In my spritekit game has more than one level. Some of the level are in landscape mode and some of are in portrait mode. Since the first level is in the landscape mode so when I call the first SKScene I set up the viewcontroller as
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
But when I want load my second scene which would be in portrait mode it shows in landscape mode. Now how do I change the orientation from landscape to portrait in the second level. There is only one view controller in my game.
Thanks in advanced.

I don't think it is possible to change the orientation of the device from code.
You could tell the users to rotate their device for the next (or current) level and handle accordingly.
Make sure your application supports multiple orientations and in the new scene, where you want to use portrait add the following code:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
I hope this helps.

Related

iOS 6 Game Center Crash on Authentication in landscape mode cocos2d

I have developed a game in cocos2d and all game screens are in Landscape mode. I am trying to implement game Center but getting crash on authentication. I did not find answer of similar type of issues. please suggest right approach...
Crash issue:-'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES'
I tried below solution but it also disturb game orientations, game starts work in portrait mode also, that i don't want:-
(NSUInteger)application:(UIApplication*)application
supportedInterfaceOrientationsForWindow: (UIWindow*)window
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
Make sure you selected landscape in Xcode summary page.
Also add these code in your viewcontroller
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Update this function in AppDelegate:
- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
return UIInterfaceOrientationMaskLandscape;
}
the solution for that is short, i spent a lot of time before finding it:
in the AppDelegate in the method didFinishLaunchingWithOptions put this line:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
obviously before call the login game center method, i put that before create the UIWindows

iphone Developing

I have 4 tabs in my application. After launching my application i want to start with landscape. Now i want to restrict one view from landscape, I tried with [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; now the view is restricting landscape and force the view into portrait then no issue right,now i will go to any other tab which are in my application they should not rotate to landscape mode.
After come back to home view where i was started my application then all are working fine.....
You need to add the following method to your UIViewController:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
If you want to allow Landscape:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
If you want to allow Portrait:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}
If Both:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}

iOS app - changing the orientation in tabbar

In my app I have a tabbar with 5 buttons. The total app has only portrait orientation and I have set it correctly.
My problem is that when I click the second button of the tabbar, the view appears normally in portrait but when the user tilts the device I want that particular view to be get changed to landscape. Is it possible to change the tabbar position to landscape and when the other buttons are clicked all to be changed to portrait?
Else while it is tilted I have to show the view alone in landscape without the tabbar, how can I do this?
So, to be clear, you want the entire app and all views in portrait EXCEPT for the view triggered by the second tab bar button, which you want to always appear in Landscape?
if I've not confused your question, than just put the following line of code in the "viewDidLoad" method of the controller of view number 2
self.view.transform = CGAffineTransformMakeRotation(M_PI / 2);
You probably want to bite the bullet and let your project accept multiple orientations at its highest level and then decide per UIViewController which orientations are supported. This will allow the frameworks to take care of a lot of the work for you (always a good idea).
In each view's controller, override this method:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
You will want to return YES for any orientation that you want the view to support.
The four options for the interfaceOrientation parameter are:
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
So for your case, you will want the view controller hosting your view and tab bar to support all of the orientations. This is how the function would look:
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return YES;
}
But for a extra detail, if you wanted to support only Landscape Left and Portrait Upside Down orientation, the function would look like this:
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
A UITabBar only supports rotation, if the ViewControllers of all tabs return YES at shouldAutorotateToInterfaceOrientation.
You could achieve what you want, if you let your "portrait" tabs check if they are visible (via
tabbar.selectedViewController
or
tabbar.selectedIndex
and only reply yes when they're not selected.
The user experience could be confusing though, when the user changes the tab in landscape mode
a portrait view is presented ...
try this w.r.t.: tabbar.selectedViewController
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeRight];

Interface Orientation won't change to Landscape on App Launch

i stuck on a problem that drives me crazy!
I have an iPad application starting with a TabBarViewController. Every Tab has it's own ViewController and nib-file.
To make interface orientation possible I added all orientations to my info.plist and subclassing my TabBarController to set:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
Every View is autoresized and formatted well for displaying the right Orientation if I rotate the Interface. If I test it in the simulator, everything looks fine and I can rotate between the Orientations.
Now the point that drives me crazy:
If I launch the App in Portrait Mode, all works fine.. but if I launch in Landscape, I get an error and my Interface orientation seems still to be Portrait, while the Simulator is in Landscape Mode!!
The Error:
2011-05-24 21:50:15.011 Project[57995:207] Using two-stage rotation animation. To use the smoother single-stage animation, this application must remove two-stage method implementations.
I checked for Orientation like this:
-(void)viewWillAppear:(BOOL)animated{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight)) {
NSLog(#"Orientation: Landscape");
}
else{
NSLog(#"Orientation: Portrait");
}
}
The Log says it is in "Landscape" Mode if I launch in Landscape, but if I change tab to another it looks terrible because the View is displayed in Portrait mode instead.
On change back to the start-view where i asked for Orientation… the log displays "Portrait"… but the Simulator is still Landscape!
I can't figure out why the Orientation is Portrait on start,…
even if I start in Landscape…
Any idea how to solve this problem?
Ok. So the documentation says viewWillAppear: is called prior to all animations. And when your app starts, its initial orientation is Portrait. Based on what your orientation is, it then rotates to that orientation. Since it animates to the orientation off screen, this must be called after viewWillAppear:/ So when viewWillAppear: is called, its still in Portrait. I tested this myself.
I solved the problem!
Simply used this in viewWillAppear method of all my Views, to make it work when started in Landscape mode:
if(([[UIApplication sharedApplication]statusBarOrientation] == UIInterfaceOrientationLandscapeLeft) || ([[UIApplication sharedApplication]statusBarOrientation] == UIInterfaceOrientationLandscapeRight)){
//Landscape View
}
else{
//Portrait View
}
Works fine for me!

Rotation Portrait Landscape with 2 XIB

i have got 2 GUIs and 2 Controllers
1 is called landscapeguicontroller and the second is called highguicontroller.
Now generally i call the highguicontroller, and when i rotate my iphone it detects that and then it shows the landscapeguicontroller:
Code:
landscapeguicontroller *neu =[[landscapeguicontroller alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:neu animated:YES];
[self dismissModalViewControllerAnimated:YES];
The Problem is that then the animation pushes the new window from the beyond side of the iphone up into the window.
In the Landscapeguicontroller,i have added to the the following lines:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
when i want go back to the highguicontroller i call:
[self dismissModalViewControllerAnimated:YES];
that all works , but just in the second animation i see the correct "rotation animation".
Have you got any suggestions?
So a short Problem description:
in the 1. animation from high to landscape, the landscape is pushed into the window
BUT in the 2. animation from landscape to high, the rotation looks like a real rotation...
i want the 1.animation look like the 2. animation
best regards
Ploetzeneder
To avoid "The Problem is that then the animation pushes the new window from the beyond side of the iphone up into the window.", try setting the view controller's modalTransitionStyle property to one of the following, whatever you prefer:
typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
} UIModalTransitionStyle;
Also, if you want to avoid the animated rotation, you can set your shouldRotate... method to disallow other orientations, but then set up to receive notifications when the device physically changes orientations, and present your modal viewcontroller when in the appropriate orientation for it. See Apple's "AlternateViews" sample code for an example of this.
The notifications reflect the physical orientation of the device, and you can receive them whether the interface is allowed to change or not. (You can look at the UIApplications's statusBarOrientation property to see what orientation the UI is in).
It sounds like you want the sequence to go like this:
Physically rotate the device from portrait to landscape
Animate the portrait view (highguicontroller) to landscape
Push the landscape view (landscapeguicontroller) up from the new "bottom" of the screen
If that's right, you'll need to have something like the following in your highguicontroller implementation:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
This will take care of step 2 (it will rotate the portrait view to landscape in either direction).
Then you'll want something like this:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if(fromInterfaceOrientation == UIInterfaceOrientationPortrait) {
[self presentModalViewController:landscapeguicontroller animated:YES];
}
else {
[self dismissModalViewControllerAnimated:YES];
}
}
That should present the landscape view after the rotation animation is complete and then dismiss it after the device is rotated back to portrait.
Hope that helps!