How to rotate the ipad screen using code in my ios app - ios

When my app is launched, it is initially in portrait orientation. It can rotate when the ipad device is physically rotated.
I hope to add a function that allows users to click a button to open a help tutorial. I want the tutorial to only be landscape.
So I override the shouldAutorotateToInterfaceOrientation method to check if the app is in help tutorial mode; returning 'Yes' if it is:
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation ==UIInterfaceOrientationLandscapeRight)`
When the screen is in portrait, and the user clicks the button to open the help tutorial, how do I rotate the screen to landscape at the beginning of my button response code?

If the controller with the help tutorial doesn't support Portrait, it will automatically show the image in Landscape mode, even if the user doesn't hold the device like that, you shouldn't need to "rotate" the screen yourself, through code.
If you want to rotate your view manually (but NOT change the device orientation), use this code for a 90 degrees rotation
CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI_2);
self.view.transform = transform;
M_PI_2 = 90 degrees
M_PI = 180 degrees

Related

Programmatically disable portrait orientation lock

In my camera app, everything works perfectly fine orientation wise. If portrait orientation lock is not enabled on a users phone, then everything works perfectly.
However, if a user has portrait orientation lock enabled and they record sideways, then the video gets recorded in portrait mode but everything is sideways inside the video.
Is there any way to check if portrait orientation lock is enabled or any way to get around this? Thank you.
There are a couple of things. You can override the shouldAutorotate function in the view controller that you are using the camera in like so:
override func shouldAutorotate() -> Bool {
return true
}
Then you could set it to false again once they are finished with the camera so it retains their device settings. Not sure if this is the best way, but is A way.
Another possible way that you might be able to get around this is to simply always check the device orientation. The device will still detect the physical orientation of the device even if the visual orientation of the UI doesn't autorotate (https://developer.apple.com/documentation/uikit/uidevice/1620053-orientation). So the following shows the possible device orientations, as per the Apple documentation (https://developer.apple.com/documentation/uikit/uideviceorientation)
enum UIDeviceOrientation : Int {
case Unknown
case Portrait // Device oriented vertically, home button on the bottom
case PortraitUpsideDown // Device oriented vertically, home button on the top
case LandscapeLeft // Device oriented horizontally, home button on the right
case LandscapeRight // Device oriented horizontally, home button on the left
case FaceUp // Device oriented flat, face up
case FaceDown // Device oriented flat, face down
}
Knowing those you can just check if the current orientation is one of those:
if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft) {
...override autorotate to true
}
These might solve your issue.
You can disable portrait orientation lock by using this method:
- (NSUInteger)lockorientation
{
return UIInterfaceOrientationMaskPortrait;
}

AutoLayout does not function correctly when phone rotated 360 degrees

I am having an issue with "autorotation". I have AutoLayout constraints that function correctly if the phone is rotated from Portrait to Landscape Left or Landscape Right. But if the phone is rotated from either landscape orientation to the other landscape orientation but going "360 degrees" instead of back into portrait first, then the layout looks like it thinks the phone is in Portrait Orientation.
I'm allowing all orientation except upside down. I've tried enabling upside-down orientation but the phone never rotates into that orientation, it just stays in landscape.
What is the appropriate way to deal with this?
I abandoned the approach using AutoLayout and instead used UIView + NSAutoLayout. This let me easily set all the constraints programmatically and everything worked much better.

How to set different orientation for different screens in corona [duplicate]

I've made a game using Corona-SDK. It has 2 scenes: (1) Menu scene and (2) Game scene.
I need my app to be supported with two orientations : 'landscapeRight' and 'landscapeLeft'.
For that, I used the following lines of code in build.settings :
orientation =
{
default = "landscapeRight",
supported =
{
"landscapeRight", "landscapeLeft"
},
}
Unfortunately, this will affect the whole app.
But I want my game scene to be supported with only one orientation(eg: landscapeRight or landscapeLeft or in portrait).
Sorry, but Corona SDK does not have such capability.
What you can do is lock into one orientation, and places where you want to support more than one orientation, you can manually check the accelerometer to see if the phone is upside down, and then manually flip your graphics.
The most easy way to flip the graphics is put them all in a single group centered on the screen, and rotate that group 180 degrees.

Opening iAds in landscape app screws the whole orientation

My app runs in landscape mode. I do not support portrait orientation and, in all of the screens, if I rotate the device to portrait, everything is, as expected, in landscape.
I am displaying iAd banners and they look good, they rotate when they should along with their superviews. Everything is ok regarding banner display.
The problem is that when I tap on them and the actual ad is opened, the whole app orientation gets screwed. The iAD is opened in portrait mode, its position is wrong, offset to half of the screen, and it messes with the whole app orientation, taking it to an unsupported and weird looking portrait mode.
Any thoughts on how to avoid this?
Some app details:
iOS6
Landscape mode only
Cocos2d + UIKit for some screens
The integration code for iAds is standard, as explained on the iAds programming guide
The app root view controller is a simple UIViewController, no navigation controllers or anything like that.
It doesn't use autolayout.
Me also used iAds in Cocos2d-landscape game. For me its working.
Here is files hosted: Download
-(void)showBannerView
{
_adBannerViewIsVisible = true;
AppController * myDelegate = (((AppController*) [UIApplication sharedApplication].delegate));
[myDelegate.navController.view addSubview:self.adBannerView];
if (self.adBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGSize s = [[CCDirector sharedDirector] winSize];
CGRect frame = self.adBannerView.frame;
frame.origin.y = s.height - frame.size.height;
frame.origin.x = (s.width/2.0f - frame.size.width/2.0f);
self.adBannerView.frame = frame;
}
completion:^(BOOL finished)
{
}];
}
}
You need to set the AdBannerView to display landscape-ads only:
adBannerView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierLandscape];
adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
This way, ads that don't support landscape should not be loaded. Be aware that this will result in a drop of your fill-rate.
Well this issue screwed me up for 2 days
and I even gave up on iAd
untill I ran to the same issue with gamecenter authentication, that always opens in portrait and screws the whole orientation for the app in the same manner .
However the solution for the game center I found almost immediately in google search.
I tried to apply the same solution for iAd and it worked.
the main idea is to let your app to run in both
portrait and landscape (in info plist of the app)
Then in your view controller to set the preffered orientation to landscape right , allow rotation of orientations and support both landscape and portrait.
The detailed answer is here:
Gamecenter authentication in landscape only Cocos2d with CCLayer for iOS 6
Special thanks to Sylvan !

Odd iPad rotation effect

I have a pretty standard iPad application that is setup to only be landscape. To affect this, I have set the initial interface orientation to landscape, and the support interface orientations to just the single landscape left home button, and also overridden shouldAutorotateToInterfaceOrientation properties to the following:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
BOOL b = UIInterfaceOrientationIsLandscape(interfaceOrientation);
return b;
}
The really odd thing is that when the app starts out, it is correct, and rotating the iPad upside down does nothing, but rotating with the home button down rotates the screen, but once rotated, it will never rotate back, so thinking this is something other than rotation settings.
Has anyone run into this before?
From the docs:
#define UIInterfaceOrientationIsLandscape(orientation) \
((orientation) == UIInterfaceOrientationLandscapeLeft || \
(orientation) == UIInterfaceOrientationLandscapeRight)
So if you want to support just ONE of the landscape rotations, this is not the way to go...

Resources