【问题标题】:Lock orientation with native Objective-C code from Qt使用 Qt 的本机 Objective-C 代码锁定方向
【发布时间】:2022-12-20 20:37:13
【问题描述】:

我希望能够仅以编程方式更改方向。按照我的意愿编写了旋转屏幕的功能。

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>

如果我这样做,用户就可以使用这两个方向。如果我想禁止这个,我必须重写this 函数,但我应该如何在 Qt 中做到这一点?

【问题讨论】:

    标签: ios objective-c qt orientation


    【解决方案1】:
        -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
                if (shouldRotate == YES){
                   return UIInterfaceOrientationMaskAll;
                }else{
                  return UIInterfaceOrientationMaskPortrait;
                }
           }
    

    在 App Delegate 中,你可以尝试添加这个函数,将 should rotate 作为全局值,当你应用方向锁定时,我们可以让 should rotate 为 false,它将处于纵向模式。如果不应用方向锁,我们可以让rotate为true,那么它可以在所有状态下旋转。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多