您好,登錄后才能下訂單哦!
這篇文章主要介紹“iOS如何實現手動和自動屏幕旋轉”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“iOS如何實現手動和自動屏幕旋轉”文章能幫助大家解決問題。
首先iPhone中屏幕分為狀態欄方向和設備方向
typedef NS_ENUM(NSInteger, UIDeviceOrientation) { UIDeviceOrientationUnknown, UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left UIDeviceOrientationFaceUp, // Device oriented flat, face up UIDeviceOrientationFaceDown // Device oriented flat, face down }; typedef NS_ENUM(NSInteger, UIInterfaceOrientation) { UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown, UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight, UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft };
系統提供兩個地方來設置設備的方向,取兩個地方的交集是最后的設備所支持的方向
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window; -(NSUInteger)supportedInterfaceOrientations;
這里需要注意的是返回的時下面的枚舉
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) { UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait), UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft), UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight), UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown), UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown), UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), };
在轉動屏幕的時候會觸發下面方法
-(BOOL)shouldAutorotate;
在該方法返回真,自動調用上面的兩個方法得到方向。
修改狀態欄方向的方法
1、使用私有API setOrientation;
2、修改狀態欄的方向,并通過設置View的transform來達到偽旋轉的結果,但是設備方向并沒有改變
3、主動出發系統支持的方法,就相當于讓這個vc在重新出來的時候系統判斷所支持的方向的機制重新走一遍。
- (void)awakeSupportInterOrtation:(UIViewController *)showVC completion:(void(^)(void))block { UIViewController *vc = [[UIViewController alloc] init]; void(^completion)() = ^() { [showVC dismissViewControllerAnimated:NO completion:^{ if (block) { block(); } }]; }; // This check is needed if you need to support iOS version older than 7.0 BOOL canUseTransitionCoordinator = [showVC respondsToSelector:@selector(transitionCoordinator)]; if (canUseTransitionCoordinator) { [showVC presentViewController:vc animated:NO completion:nil]; [showVC.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) { completion(); }]; } else { [showVC presentViewController:vc animated:NO completion:completion]; } } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } -(BOOL)shouldAutorotate { return YES; }
在需要轉為豎屏的時候調用一個方法,在后面兩個方法中如上實現,第二個方法中返回的是你最終要轉向的方向。
關于“iOS如何實現手動和自動屏幕旋轉”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。