我有一个使用Cocos2dx引擎开发的IOS应用程序。
针对特定方向锁定应用程序(例如肖像) 除了最近的应用程序栏和根据设备方向的通知外,应用程序中的所有内容似乎都能正常工作,并且根据正确的方向。
我希望能够限制它,使其与应用程序本身具有相同的方向。
我注意到删除info.plist
文件中的横向方向可以完成工作,但我希望能够通过代码完成。
在IOS 6中,我发现我只需覆盖referredInterfaceOrientationForPresentation
RootViewController
中的RootViewController
并给出正确的方向,这就是诀窍,
但是这种方法不适用于IOS 5及以下版本。
在使用IOS 5及更低版本的设备上,我需要做些什么才能使用?
这是#include "cocos2d.h"
#import "RootViewController.h"
#import "AppDelegate.h"
#import "misc/deviceOrientation.h"
#import "services/ios/ConfigurationServiceImpl.h"
@implementation RootViewController
@synthesize progressView;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
-(NSUInteger)supportedInterfaceOrientations{
ConfigurationServiceImpl* configurationService = [ConfigurationServiceImpl instance];
if ([configurationService isLandscape])
return UIInterfaceOrientationMaskLandscape;
else
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
-(BOOL)shouldAutorotate{
return YES;
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// printf("shouldAutorotateToInterfaceOrientation\n");
//
// 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
//
return ( interfaceOrientation == UIInterfaceOrientationPortrait );
#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
ConfigurationServiceImpl* configurationService = [ConfigurationServiceImpl instance];
return [configurationService shouldAutorotateToInterfaceOrientation: 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
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
CDeviceOrientation::setDeviceOrientation(CDeviceOrientation::left);
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
CDeviceOrientation::setDeviceOrientation(CDeviceOrientation::right);
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
CDeviceOrientation::setDeviceOrientation(CDeviceOrientation::down);
else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
CDeviceOrientation::setDeviceOrientation(CDeviceOrientation::up);
}
#endif // GAME_AUTOROTATION == kGameAutorotationUIViewController
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
ConfigurationServiceImpl* configurationService = [ConfigurationServiceImpl instance];
if ([configurationService isLandscape])
return UIInterfaceOrientationLandscapeRight;
else
return UIInterfaceOrientationPortrait;
}
@end
的代码(我没有写它,我刚刚添加了最后一种方法,我试图弄清楚如何修复通知和最近的应用程序问题)
{{1}}
答案 0 :(得分:1)
使用适用于iOS 5的shouldAutorotateToInterfaceOrientation和适用于iOS 6的shouldAutorotate。在iOS5方法中,使用if case作为支持的方向并为它们返回YES。在您的应用摘要中,启用所有方向。希望这些能帮到你。
答案 1 :(得分:0)
我制作这个代码骨架来处理想要的&不需要的设备方向,在我的情况下,我想忽略UIDeviceOrientationUnknown,UIDeviceOrientationFaceUp和UIDeviceOrientationFaceDown,缓存最后允许的方向。此代码适用于iPhone和iPad设备,对您有用。
- (void)modXibFromRotation {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSString *device = [[UIDevice currentDevice]localizedModel];
UIInterfaceOrientation cachedOrientation = [self interfaceOrientation];
if ([device isEqualToString:@"iPad"]) {
if (orientation == UIDeviceOrientationUnknown ||
orientation == UIDeviceOrientationFaceUp ||
orientation == UIDeviceOrientationFaceDown) {
orientation = (UIDeviceOrientation)cachedOrientation;
}
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
/* Your code */
}
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
/* Your code */
}
}
if ([device isEqualToString:@"iPhone"] || [device isEqualToString:@"iPod"]) {
if (orientation == UIDeviceOrientationUnknown ||
orientation == UIDeviceOrientationFaceUp ||
orientation == UIDeviceOrientationFaceDown) {
orientation = (UIDeviceOrientation)cachedOrientation;
}
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
/* Your code */
}
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
/* Your code */
}
}
}
哟必须从视图控制器中的两个位置调用此方法:
拳头开始在您的App代理中生成设备Orientation Notifications:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//**** ADD THIS CODE *****
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
// Add the main view controller's view to the window and display.
self.window.rootViewController = self.mainViewController;
[self.window makeKeyAndVisible];
return YES;
}
然后在视图控制器中侦听设备Orientation Notifications:
- (void)viewDidLoad {
[notificationCent addObserver:self
selector:@selector(modXibFromRotation)
name:UIDeviceOrientationDidChangeNotification object:nil];
}
最后从:
调用modXibFromRotation方法- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear];
[self modXibFromRotation];
}
这将在显示视图之前调用check方法。