ts:
// "V" "H"
setOrientation(dir)
{
cc.director.pause();
let methodName = "setOrientation";
if (cc.sys.os == cc.sys.OS_WINDOWS) {
} else if (cc.sys.os == cc.sys.OS_IOS) {
methodName = methodName + ":";
jsb.reflection.callStaticMethod("AppController", methodName, dir);
} else if (cc.sys.os == cc.sys.OS_ANDROID) {
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", methodName, "(Ljava/lang/String;)V",dir);
}
let frameSize = cc.view.getFrameSize();
if (dir == "V") {//竖屏
if (frameSize.width > frameSize.height){
cc.view.setFrameSize(frameSize.height,frameSize.width);
}
cc.view.setDesignResolutionSize(720, 1280, cc.ResolutionPolicy.FIXED_HEIGHT);
}else if(dir == "H") {//横屏
if (frameSize.height > frameSize.width){
cc.view.setFrameSize(frameSize.height,frameSize.width);
}
cc.view.setDesignResolutionSize(1280, 720, cc.ResolutionPolicy.FIXED_HEIGHT);
}
cc.director.resume();
}
Android:
public static AppActivity s_activity = null;
s_activity = this;
public static void setOrientation(String dir)
{
if (dir.equals("H")) {
s_activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
} else if (dir.equals("V")) {
s_activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
}
ios:
ccc选中方向:
xcode选中方向:
AppController.mm
static AppController* s_sharedAppController;
s_sharedAppController = self;
+ (void)setOrientation:(NSString*)dir
{
if ([dir isEqualToString:@"H"]) {
s_sharedAppController->_viewController.orientationMask = UIInterfaceOrientationMaskLandscapeRight;
s_sharedAppController->_viewController.orientation = UIInterfaceOrientationLandscapeRight;
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationUnknown] forKey:@"orientation"];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
} else if ([dir isEqualToString:@"V"]) {
s_sharedAppController->_viewController.orientationMask = UIInterfaceOrientationMaskPortrait;
s_sharedAppController->_viewController.orientation = UIInterfaceOrientationPortrait;
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationUnknown] forKey:@"orientation"];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
}
}
RootViewController.h
@property(nonatomic, assign) UIInterfaceOrientationMask orientationMask;
@property(nonatomic, assign) UIInterfaceOrientation orientation;
RootViewController
_orientationMask = UIInterfaceOrientationMaskPortrait;
_orientation = UIInterfaceOrientationPortrait;
// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead
#ifdef __IPHONE_6_0
- (NSUInteger) supportedInterfaceOrientations{
//return UIInterfaceOrientationMaskAllButUpsideDown;
return _orientationMask;
}
#endif
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return _orientation;
}