How to rotate landscape only with Cocos2d - ios

This is my last step of the app, everything else is done.
I'm using cocos2d, and no matter what I do, I cannot get the app to rotate. It HAS to only rotate landscape with the way my app is set up.
Here's my GameConfig.h
#ifndef __GAME_CONFIG_H
#define __GAME_CONFIG_H
//
// Supported Autorotations:
// None,
// UIViewController,
// CCDirector
//
#define kGameAutorotationNone 0
#define kGameAutorotationCCDirector 1
#define kGameAutorotationUIViewController 2
//
// Define here the type of autorotation that you want for your game
//
#define GAME_AUTOROTATION kGameAutorotationUIViewController
#endif // __GAME_CONFIG_H
And my AppDelegate's applicationDidFinishLaunching method,
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
//[director setDeviceOrientation:kCCDeviceOrientationPortrait];
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#else
// [director setDeviceOrientation:kCCDeviceOrientationPortrait];
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#endif
And RootController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return ( UIInterfaceOrientationIsLandscape( interfaceOrientation ) );
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//
// Assuming that the main window has the size of the screen
// BUG: This won't work if the EAGLView is not fullscreen
///
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect rect = CGRectZero;
if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
rect = screenRect;
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );
CCDirector *director = [CCDirector sharedDirector];
EAGLView *glView = [director openGLView];
float contentScaleFactor = [director contentScaleFactor];
if( contentScaleFactor != 1 ) {
rect.size.width *= contentScaleFactor;
rect.size.height *= contentScaleFactor;
}
glView.frame = rect;
}
In the Info.plist I have both landscape orientations in the supported list.
What am I doing wrong?
Thanks

Try to add this line
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
to your applicationDidFinishLaunching: method

Related

My app display black screen when I rotate from Landscapeleft to LandscapeRight and LandscapeRight to LandscapeLeft

Hi I am new to iOS developer.
I have a problem with my app when I rotate LandscapeLeft to LandscapeRight my app display shows black screen.
I am using this method for rotate.
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect rect = CGRectZero;
if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
rect = screenRect;
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );
CCDirector *director = [CCDirector sharedDirector];
EAGLView *glView = [director openGLView];
float contentScaleFactor = [director contentScaleFactor];
if( contentScaleFactor != 1 ) {
rect.size.width *= contentScaleFactor;
rect.size.height *= contentScaleFactor;
}
glView.frame = rect;
}

Game not rotating PortraitUpsideDown

The orientation is shown in 'support interface orientations' in the info.plist file, however still does not rotate when turned.
I am looking to make Portrait & Portraitupsidedown
This is from the RootViewController.m
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//
// There are 2 ways to support auto-rotation:
// - The OpenGL / cocos2d way
// - Faster, but doesn't rotate the UIKit objects
// - The ViewController way
// - A bit slower, but the UiKit objects are placed in the right place
//
#if GAME_AUTOROTATION==kGameAutorotationNone
//
// EAGLView won't be autorotated.
// Since this method should return YES in at least 1 orientation,
// we return YES only in the Portrait orientation
//
#elif GAME_AUTOROTATION==kGameAutorotationCCDirector
//
// EAGLView will be rotated by cocos2d
//
// Sample: Autorotate only in landscape mode
//
if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
[[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeRight];
} else if( interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeLeft];
}
// Since this method should return YES in at least 1 orientation,
// we return YES only in the Portrait orientation
return ( interfaceOrientation == UIInterfaceOrientationPortrait );
#elif GAME_AUTOROTATION == kGameAutorotationUIViewController
//
// EAGLView will be rotated by the UIViewController
//
// Sample: Autorotate only in landscpe mode
//
// return YES for the supported orientations
return ( UIInterfaceOrientationIsPortrait(interfaceOrientation));
#else
#error Unknown value in GAME_AUTOROTATION
#endif // GAME_AUTOROTATION
// Shold not happen
return NO;
}
//
// This callback only will be called when GAME_AUTOROTATION == kGameAutorotationUIViewController
//
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//
// Assuming that the main window has the size of the screen
// BUG: This won't work if the EAGLView is not fullscreen
///
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect rect = CGRectZero;
if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
rect = screenRect;
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );
CCDirector *director = [CCDirector sharedDirector];
EAGLView *glView = [director openGLView];
float contentScaleFactor = [director contentScaleFactor];
if( contentScaleFactor != 1 ) {
rect.size.width *= contentScaleFactor;
rect.size.height *= contentScaleFactor;
}
glView.frame = rect;
}
#endif // GAME_AUTOROTATION == kGameAutorotationUIViewController
From the AppDelegate.m file I also have;
// Init the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Try to use CADisplayLink director
// if it fails (SDK < 3.1) use the default director
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector *director = [CCDirector sharedDirector];
// Init the View Controller
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
//
// Create the EAGLView manually
// 1. Create a RGB565 format. Alternative: RGBA8
// 2. depth format of 0 bit. Use 16 or 24 bit for 3d effects, like CCPageTurnTransition
//
//
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGB565 // kEAGLColorFormatRGBA8
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];
// attach the openglView to the director
[director setOpenGLView:glView];
// // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [director enableRetinaDisplay:YES] )
CCLOG(#"Retina Display Not supported");
//
// VERY IMPORTANT:
// If the rotation is going to be controlled by a UIViewController
// then the device orientation should be "Portrait".
//
// IMPORTANT:
// By default, this template only supports Landscape orientations.
// Edit the RootViewController.m file to edit the supported orientations.
//
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
[director setAnimationInterval:1.0/60];
[director setDisplayFPS:NO];
// make the OpenGLView a child of the view controller
[viewController setView:glView];
// make the View Controller a child of the main window
[window addSubview: viewController.view];
[window makeKeyAndVisible];
I have other games, using Cocos2d that I have managed to get to rotate and I cross-checked the settings with those, they are the same, so I cannot figure out why it is not rotating?
EDIT -
This could be a telling message about this...
Application windows are expected to have a root view controller at the end of application launch
Try adding rootViewController
[window setRootViewController: viewController];
In your rootview controller, comment out
CCDirector *director = [CCDirector sharedDirector];
EAGLView *glView = [director openGLView];
float contentScaleFactor = [director contentScaleFactor];
if( contentScaleFactor != 1 ) {
rect.size.width *= contentScaleFactor;
rect.size.height *= contentScaleFactor;
}
glView.frame = rect;

alignment for different ios device

I am new to iOS programming. I made an application for iPhone retina 4-inch. Now I want to customise my code in order for it to run on an iPhone retina-3.5 inch and iPhone 5.
I tried to create an if query for self.view.frame.size.width and self.view.frame.size.height, but I get a "Not assignable" error.
Can anyone tell me how to specify conditions for setting up the workspace nicely in all devices? If possible please give me the exact screen-size of all that devices.
You can use this:
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
References:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIScreen_Class/Reference/UIScreen.html
You look at the device's screen size (in points) and from that surmise if it's an iPad or iPhone etc., and then use hard-coded values for the screen sizes.
Here's some code to get the screen size:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
Be aware that width and height might be swapped, depending on device orientation.
to compare screen size include this macro and use it any where necessary
#define isPhone5 ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568)
if you want to compare screen sizes only then reduce the above macro to
#define isiPhone5 ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE
For example in your .m file
#import "ViewController.h"
#define isiPhone5 ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE //hear u put the macro and use it anywhere in this file
#interface ViewController ()<FootballPlayerDelegate>//confirms to this delegate
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
if(isiPhone5)
{
NSLog(#"I am 4inch screen");
}
else
{
NSLog(#"I am 3.5inch screen");
}
// Do any additional setup after loading the view, typically from a nib.
}
Try this In your .M file:
-(void)viewWillAppear:(BOOL)animated
{
[self willAnimateRotationToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:1.0];
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft)
{
[self setFrameForLeftAndRightOrientation];
}
else if(orientation == UIInterfaceOrientationPortrait )
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
- (void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
[self setFrameForLeftAndRightOrientation];
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
-(void)setFrameForLeftAndRightOrientation
{
if(IS_IPAD)
{
//Write here code for iPad (Landscape mode)
}
else if(IS_IPHONE)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
//Write code here for iPhone 5 (Landscape mode)
}
else
{
//Write code here for iPhone(3.5 inch) (Landscape mode)
}
}
}
-(void)setFrameForPortraitAndUpsideDownOrientation
{
if(IS_IPAD)
{
}
else if(IS_IPHONE)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
}
else
{
}
}
}
#define IS_IPHONE_SIMULATOR ([[[UIDevice currentDevice]model] isEqualToString : #"iPhone Simulator"])
#define IS_IPHONE ([[[UIDevice currentDevice]model] isEqualToString : #"iPhone"])
#define IS_IPOD_TOUCH ([[[UIDevice currentDevice]model] isEqualToString : #"iPod Touch"])
#define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
#define ITS_IPHONE5 ( IS_HEIGHT_GTE_568 )
if (ITS_IPHONE5)
{
}
else
{
}
Use This Code in project-Prefix.pch file in supporting file folder.
#define IS_IPHONE_SIMULATOR ([[[UIDevice currentDevice]model]
isEqualToString : #"iPhone Simulator"])
#define IS_IPHONE ([[[UIDevice currentDevice]model] isEqualToString : #"iPhone"])
#define IS_IPOD_TOUCH ([[[UIDevice currentDevice]model] isEqualToString : #"iPod Touch"])
#define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
#define ITS_IPHONE5 ( IS_HEIGHT_GTE_568 )
Copy and Past This Code After That in your .m File past the first code i give you...
Dont need to write size on viewdidload write only in these cots
if (IS_IPAD)
{
}
else if(IS_IPHONE)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
}
else
{
}
}

set the frame size when orientation is changed in ios

I want to change the orientation of a view in ios 6. When the orientation is changed, I want to set the frame size of the views.
My Sample Code for orientation change:
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSInteger)supportedInterfaceOrientations
{
NSInteger mask = 0;
intOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(intOrientation == UIInterfaceOrientationPortrait || intOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
mask |= UIInterfaceOrientationMaskPortrait;
} else
if(intOrientation == UIInterfaceOrientationLandscapeLeft)
{
mask |= UIInterfaceOrientationMaskLandscapeLeft;
}
}
NSLog (#"mask %d",mask);
return mask;
}
// Here now mask value is 2 because my default mode is portrait.
Now when I change my orientation to landscape, I want to set the frame size. Please guide me on how to do that.
ok..Can I set the frame size for the view like dis by taking the version and comparing it in viewdidLoad:
orientation = [[UIDevice currentDevice] orientation];
NSString *reqSysVer = #"6.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
NSLog (#"ios version %#",currSysVer);
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
{
if (orientation == UIInterfaceOrientationMaskPortrait) {
self.view.frame = CGRectMake(0, 0, 1000, 800);
self.tableView1.frame = CGRectMake(1, 0, 764,510);
}
else if (orientation == UIInterfaceOrientationMaskLandscapeLeft)
{
self.view.frame = CGRectMake(0, 0, 1300, 370);
self.tableView1.frame = CGRectMake(0, 0, 1300, 370);
self.view.backgroundColor = [UIColor blackColor];
}
}
you can get a notification when the interface orientation changes:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
and add a function like:
- (void)didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientation...)
{
// add some code
}
}
The best solution would be to use Auto Layout or Constraints, I think.
But you can also check for the device orientation using
[UIDevice currentDevice].orientation
or check the size (e.g. for iPhone 5 support) using
[UIScreen mainScreen].bounds.size.height
// Update:
Consider that on iOS 8 width and height were switched in landscape. So witdh and height are always depending on the orientation now.

My modal view does not rotate

I encounter an issue with a modal view display. My first view rotate very well. The controller of the modal view implements the "ShouldAutorotationToInterfaceRotation" method but when I rotate the device or simulator the modal view doesn't rotate.
I've added some comments to check if the first view detects the rotation events instead of the modal view but no, it doesn't get the event.
Do you have a clue?
You will need to subscribe to NSNotificationCenter to receive rotation events for your modal view. Then, if you want to keep the modal view orientated correctly relative to the bottom view, one solution would be to transform the view. Here's one implementation:
Put this in the viewDidLoad and save the original orientation as an angle:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
int initialOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (initialOrientation == UIDeviceOrientationPortrait)
initialAngle = 0.0;
else if (initialOrientation == UIDeviceOrientationLandscapeRight)
initialAngle = 90.0;
else if (initialOrientation == UIDeviceOrientationPortraitUpsideDown)
initialAngle = 180.0;
else if (initialOrientation == UIDeviceOrientationLandscapeLeft)
initialAngle = 270.0;
currentAngle = initialAngle;
Then define the function orientationChanged:(NSNotification *)notification with code to rotate correctly relative to the initial orientation:
int newOrientation = [[notification object] orientation];
if (newOrientation == UIDeviceOrientationPortrait)
desiredAngle = 0.0;
else if (newOrientation == UIDeviceOrientationLandscapeRight)
desiredAngle = 90.0;
else if (newOrientation == UIDeviceOrientationPortraitUpsideDown)
desiredAngle = 180.0;
else if (newOrientation == UIDeviceOrientationLandscapeLeft)
desiredAngle = 270.0;
if(desiredAngle != currentAngle)
{
CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI * (-desiredAngle+initialAngle) / 180.0);
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, 0);
[[self releaseView] setTransform:CGAffineTransformConcat(translate, rotate)];
}
currentAngle = desiredAngle;

Resources