RSS订阅 | 匿名投稿
您的位置:网站首页 > FMX > 正文

delphi APP 横屏和竖屏

作者:admin 来源: 日期:2019/4/15 14:05:01 人气: 标签:

普通情况下,APP 随着用户拿手机是横向还是竖向,自动翻转 -- 当然,这个也需要用户设置手机的屏幕是否跟随手机的物理方向而翻转。

但如果我们的APP,在某种情况下,必须以竖屏方式显示,在某种情况下,必须以横屏方式显示,不管用户是否把手机横过来放。该怎么办?

网上搜了一堆东西出来。安卓的资料比较多一点,iOS 的就很少了。最终解决了这个问题。大概代码如下:


uses {$IFDEF IOS}iOSapi.UIKit{$ENDIF};

-----------------------------


procedure LockOrientations_iOS(ATo: TScreenOrientation; APossibles: TScreenOrientations);
{$IFDEF IOS}
var
  win : UIWindow;
  App : UIApplication;
  new : UIViewController;
  old : UIViewController;
  toio: UIInterfaceOrientation;
{$ENDIF}
begin
{$IFDEF IOS}
  Application.FormFactor.Orientations := [];
  App := TUIApplication.Wrap(TUIApplication.OCClass.sharedApplication);
  win := TUIWindow.Wrap(App.windows.objectAtIndex(0));
  case ATo of
    TScreenOrientation.Portrait: toio := UIInterfaceOrientationPortrait;
    TScreenOrientation.Landscape: toio := UIInterfaceOrientationLandscapeLeft;
    TScreenOrientation.InvertedLandscape: toio := UIInterfaceOrientationLandscapeRight;
    else //TScreenOrientation.InvertedPortrait
      toio := UIInterfaceOrientationPortraitUpsideDown;
  end;
  App.setStatusBarOrientation(toio);
  Application.FormFactor.Orientations := APossibles;
  new := TUIViewController.Wrap(TUIViewController.alloc.init);
  old := win.rootViewController;
  Win.setRootViewController(new);
  Win.makeKeyAndVisible;
  win.setRootViewController(old);
  win.makeKeyAndVisible;
{$ENDIF}
end;

----------------------

function ChangeScreenOrientation(const AIsLandscap: Boolean = true): Integer;

function ChangeScreenOrientation(const AIsLandscap: Boolean = true): Integer;
var
  ScreenService: IFMXScreenService;
  OrientSet: TScreenOrientations;
{$IFDEF IOS}
  screenSet: TScreenOrientation;
{$ENDIF}
begin
{$IFDEF ANDROID}
  //强制横屏。这段代码对安卓有用。网上看到的说法是对 iOS 无用。需要对 iOS 进行测试。pcplayer
  if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService)) then
  begin
    if AIsLandscap then //横屏显示
      OrientSet := [TScreenOrientation.soLandscape]
    else
      OrientSet := [TScreenOrientation.Portrait];
    ScreenService.SetScreenOrientation(OrientSet);
  end;
{$ENDIF}

{$IFDEF IOS}
  if AIsLandscap then //横屏显示
  begin
    OrientSet := [TScreenOrientation.soLandscape];
    screenSet := TScreenOrientation.Landscape;
  end
  else
  begin
    OrientSet := [TScreenOrientation.Portrait];
    screenSet := TScreenOrientation.Portrait;
  end;
  LockOrientations_iOS(screenSet, OrientSet);
{$ENDIF}
end;


使用:程序调用 ChangeScreenOrientation 这个方法,可以设置横屏或竖屏。iOS 和 Android 都可以。


读完这篇文章后,您心情如何?
0
0
0
0
0
0
0
0
本文网址:
下一篇:没有资料