[英]Lock orientation with native Objective-C code from Qt
我希望能夠僅以編程方式更改方向。 按照我的意願編寫了旋轉屏幕的功能。
UIInterfaceOrientation toIOSOrientation(ScreenOrientation desiredOrientaion) {
if (desiredOrientaion == ScreenOrientation::Landscape || desiredOrientaion == ScreenOrientation::LandscapeReversed) {
return UIInterfaceOrientation::UIInterfaceOrientationLandscapeLeft;
} else if (desiredOrientaion == ScreenOrientation::Portrait) {
return UIInterfaceOrientation::UIInterfaceOrientationPortrait;
} else {
return UIInterfaceOrientation::UIInterfaceOrientationPortrait;
}
}
void iOSTools::changeOrientation(ScreenOrientation desiredOrientaion) {
NSNumber* value = [NSNumber numberWithInt:toIOSOrientation(desiredOrientaion)];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
}
但是我必須在應用程序清單中打開我的兩個方向:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
</array>
如果我這樣做,用戶就可以使用這兩個方向。 如果我想禁止這個,我必須覆蓋這個函數,但我應該如何在 Qt 中做到這一點?
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if (shouldRotate == YES){
return UIInterfaceOrientationMaskAll;
}else{
return UIInterfaceOrientationMaskPortrait;
}
}
在 App Delegate 中,你可以嘗試添加這個函數,將 should rotate 作為全局值,當你應用方向鎖定時,我們可以讓 should rotate 為 false,它將處於縱向模式。 如果不應用方向鎖,我們可以讓rotate為true,那么它可以在所有狀態下旋轉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.