【问题标题】:Delphi/Firemonkey Change iOS screen rotation at runtimeDelphi/Firemonkey 在运行时更改 iOS 屏幕旋转
【发布时间】:2016-01-28 00:15:58
【问题描述】:

基本上我想要实现的是当用户在应用程序的某个部分根据需要更改屏幕旋转时,我为 Andriod 工作,但我不明白为什么它不适用于 iOS

procedure TForm1.Button1Click(Sender: TObject);
var
  ScreenService: IFMXScreenService;
  OrientSet: TScreenOrientations;
begin
    if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService)) 
    then
    begin
        OrientSet := [TScreenOrientation.soLandscape];//<- Break point set here and line is executed
        ScreenService.SetScreenOrientation(OrientSet);
    end;
end;

取自这里:How to prevent screen rotation with android development in delphi xe5 Firemonkey

ScreenService.SetScreenOrientation 被执行并且不会引发异常但方向没有改变,我还在 Project>Options>Application>Orientation 中设置了 Enable customorientation 但这也没有有什么作用。

让我感到奇怪的是,如果它不被支持,那么不应该这样

if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService)) 

返回假?甚至没有进入开始

在我将其设置为横向后,我添加了一个测试按钮来检查屏幕方向

if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService)) 
then
begin
    case ScreenService.GetScreenOrientation of
        TScreenOrientation.Portrait: ShowMessage('Portrait');
        TScreenOrientation.Landscape: ShowMessage('landscape');
        TScreenOrientation.InvertedPortrait: ShowMessage('Inverted-Portrait');
        TScreenOrientation.InvertedLandscape: ShowMessage('Inverted-Landscape');
        else ShowMessage('not set');
    end;
end;

如果它在设置为横向后是纵向的,它仍然会显示纵向

更新 1:我也尝试过更改

OrientSet := [TScreenOrientation.soLandscape] // <- Deprecated

OrientSet := [TScreenOrientation.Landscape]

但是行为还是一样的

【问题讨论】:

    标签: ios delphi firemonkey delphi-10-seattle


    【解决方案1】:

    好的,这次轮换让我深入研究了 iOS API,以了解 iOS 如何管理方向。

    因为您不能在 iOS 中假装旋转或强制设备确定 设备方向 在 iOS 中,您需要考虑很多方向

    • 设备方向
    • 状态栏方向
    • UIViewcontroller 方向

    无法强制或更改设备方向,这将始终显示您的设备现在所处的方向。

    然而,状态栏方向始终有 setStatusBarOrientation 过程,您可以调用并指定您想要的方向,但这被标记为已弃用,因此不起作用,但是当我调用时我没有得到外部异常该功能表明它仍然存在,只是无法正常工作。

    然后我读到了这个:

    setStatusBarOrientation:animated: 方法并未完全弃用。它现在只有在最顶层全屏视图控制器的 supportedInterfaceOrientations 方法返回 0 时才有效

    然后我决定试试这个

    Application.FormFactor.Orientations := []; //the supportedInterfaceOrientations returns 0
    App := TUIApplication.Wrap(TUIApplication.OCClass.sharedApplication);
    win := TUIWindow.Wrap(App.windows.objectAtIndex(0));
    App.setStatusBarOrientation(UIInterfaceOrientationLandscapeLeft);
    

    它适用于状态栏方向的变化,但如上所述,此协议已被弃用,因此在某个阶段它可能/将会消失,这将不再有效。

    但这仅改变了状态栏和所有警报视图等的旋转,但在我想要更改为横向的情况下,rootviewcontroller 中的实际内容仍在纵向中。

    然后我想如果我在 Delphi 中会去 Application->Orientation-> Enable custom rotation and say landscape only then the App will only display in landscape,它做得很完美,因为当创建表单时rootViewcontroller是使返回的supportedInterface方向仅为横向,而Viewcontroller转到横向。

    因为当您的设备更改设备方向时,会根据 Viewcontroller 支持的接口方向评估方向,如果不支持方向,则根本不旋转。

    但是,当分配了新的 rootviewcontroller 时,必须更新 GUI 以显示 Viewcontrollers 支持的方向,考虑到这一点,这是我的修复/hack,将您的应用程序的方向更改为您想要的方向。

    TL;DR

    Uses: iOSapi.UIKit; 
    
    procedure ChangeiOSorientation(toOrientation: UIInterfaceOrientation;
    possibleOrientations: TScreenOrientations);
    var
        win : UIWindow;
        App : UIApplication;
        viewController : UIViewController;
        oucon: UIViewController;
    begin
        Application.FormFactor.Orientations := []; //Change supported orientations
        App := TUIApplication.Wrap(TUIApplication.OCClass.sharedApplication);
        win := TUIWindow.Wrap(App.windows.objectAtIndex(0)); //The first Windows is always the main Window
    
        App.setStatusBarOrientation(toOrientation);
        {After you have changed your statusbar orientation set the
        Supported orientation/orientations to whatever you need}
        Application.FormFactor.Orientations := possibleOrientations;
        viewController := TUIViewController.Wrap(TUIViewController.alloc.init);//dummy ViewController
        oucon := TUIViewController.Wrap(TUIViewController.alloc.init);
        {Now we are creating a new Viewcontroller now when it is created
         it will have to check what is the supported orientations}
        oucon := win.rootViewController;//we store all our current content to the new ViewController
        Win.setRootViewController(viewController);
        Win.makeKeyAndVisible;// We display the Dummy viewcontroller
    
        win.setRootViewController(oucon);
        win.makeKeyAndVisible; 
        {And now we Display our original Content in a new Viewcontroller 
         with our new Supported orientations} 
    end;
    

    您现在只需致电ChangeiOSorientation(toOrientation,possibleOrientations)

    如果您希望它转到纵向但可以选择横向

     ChangeiOSorientation(UIInterfaceOrientationPortrait,[TScreenOrientation.Portrait,TScreenOrientation.Landscape,TScreenOrientation.InvertedLandscape]);    
    

    正在处理中

    • 西雅图德尔福 10 号
    • 运行 iOS 9.0 的 iPhone 5
    • PA 服务器 17.0
    • Xcode 7.1
    • iPhoneOS 9.1 SDK

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 2019-05-05
      • 1970-01-01
      • 1970-01-01
      • 2022-07-15
      相关资源
      最近更新 更多