gpt4 book ai didi

ios - 在 IOS 5 上将应用程序限制为特定方向

转载 作者:行者123 更新时间:2023-11-29 03:47:30 24 4
gpt4 key购买 nike

我有一个使用 Cocos2dx 引擎开发的 IOS 应用程序。

应用程序被锁定为特定方向(例如纵向)应用程序中的所有内容似乎都工作正常,并且方向正确,但最近的应用程序栏和通知除外,它们根据设备方向。

我希望能够限制它,使其具有与应用程序本身相同的方向。

我注意到删除 info.plist 文件中的横向可以完成这项工作,但我希望能够通过代码来完成。

在 IOS 6 中,我发现我所要做的就是覆盖 RootViewController 中的 referredInterfaceOrientationForPresentation并给出正确的方向,这样就可以了,但此方法不适用于IOS 5及以下版本。

我需要做什么才能使其在 iOS 5 及更低版本的设备上运行?

这是RootViewController的代码(我没有编写它,我只是添加了最后一个方法,我正在尝试找出如何修复通知和最近的应用程序问题)

#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

最佳答案

对于iOS 5使用shouldAutorotateToInterfaceOrientation,对于iOS 6使用shouldAutorotate。在iOS5方法中,使用if case表示您支持的方向并为其返回YES。在您的应用程序摘要中,启用所有方向。希望这些对您有帮助。

关于ios - 在 IOS 5 上将应用程序限制为特定方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17527107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com