您的位置:首页 > 移动开发 > IOS开发

增强版——Firemonkey运行时更改锁定iOS显示方向

2016-03-12 18:27 344 查看
之前在这里介绍过Firemonkey如何在运行时更改和锁定iOS设备的显示方向。这里给出一个改良优化的版本,增加考虑原先支持显示方向以减少不必要的重绘。

下载代码

unit TU2.iOS.Helper;

interface

uses FMX.Types;

//更改屏幕方向
procedure ChangeOrientations(APreferred: TScreenOrientation; ASupported: TScreenOrientations);

implementation

uses
iOSapi.UIKit, FMX.Platform, FMX.Forms;

function GetOrientation(AOrientation: UIInterfaceOrientation): TScreenOrientation; overload;
begin
case AOrientation of
UIInterfaceOrientationPortrait: Result := TScreenOrientation.Portrait;
UIInterfaceOrientationLandscapeLeft: Result := TScreenOrientation.Landscape;
UIInterfaceOrientationLandscapeRight: Result := TScreenOrientation.InvertedLandscape;
else
Result := TScreenOrientation.InvertedPortrait;
end;
end;

function GetOrientation(AOrientation: TScreenOrientation): UIInterfaceOrientation; overload;
begin
case AOrientation of
TScreenOrientation.Portrait: Result := UIInterfaceOrientationPortrait;
TScreenOrientation.Landscape: Result := UIInterfaceOrientationLandscapeLeft;
TScreenOrientation.InvertedLandscape: Result := UIInterfaceOrientationLandscapeRight;
else //TScreenOrientation.InvertedPortrait
Result := UIInterfaceOrientationPortraitUpsideDown;
end;
end;

procedure ChangeOrientations(APreferred: TScreenOrientation; ASupported: TScreenOrientations);
var
App : UIApplication;
win : UIWindow;
new,old : UIViewController;
begin
Include(ASupported,APreferred);
App := TUIApplication.Wrap(TUIApplication.OCClass.sharedApplication);
if not (GetOrientation(App.statusBarOrientation) in ASupported) then
App.setStatusBarOrientation(GetOrientation(APreferred));//Change stausbar orientations
//Change Supported orientations
if Application.FormFactor.Orientations<>ASupported then
begin
//The first Windows is always the main Window
win := TUIWindow.Wrap(App.windows.objectAtIndex(0));
//we store all our current content to the old ViewController
old := win.rootViewController;
{Now we are creating a new(dummy) Viewcontroller, after it is created,
it will have to check what is the supported orientations}
Application.FormFactor.Orientations := ASupported;
new := TUIViewController.Wrap(TUIViewController.alloc.init);
Win.setRootViewController(new);
Win.makeKeyAndVisible;// We display the Dummy viewcontroller
{And now we Display our original Content in a new Viewcontroller
with our new Supported orientations}
win.setRootViewController(old);
win.makeKeyAndVisible;
end;
end;

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: