您好,登錄后才能下訂單哦!
這篇文章主要介紹“iOS如何實現(xiàn)手動和自動屏幕旋轉(zhuǎn)”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“iOS如何實現(xiàn)手動和自動屏幕旋轉(zhuǎn)”文章能幫助大家解決問題。
首先iPhone中屏幕分為狀態(tài)欄方向和設備方向
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 };
系統(tǒng)提供兩個地方來設置設備的方向,取兩個地方的交集是最后的設備所支持的方向
-(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), };
在轉(zhuǎn)動屏幕的時候會觸發(fā)下面方法
-(BOOL)shouldAutorotate;
在該方法返回真,自動調(diào)用上面的兩個方法得到方向。
修改狀態(tài)欄方向的方法
1、使用私有API setOrientation;
2、修改狀態(tài)欄的方向,并通過設置View的transform來達到偽旋轉(zhuǎn)的結(jié)果,但是設備方向并沒有改變
3、主動出發(fā)系統(tǒng)支持的方法,就相當于讓這個vc在重新出來的時候系統(tǒng)判斷所支持的方向的機制重新走一遍。
- (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; }
在需要轉(zhuǎn)為豎屏的時候調(diào)用一個方法,在后面兩個方法中如上實現(xiàn),第二個方法中返回的是你最終要轉(zhuǎn)向的方向。
關于“iOS如何實現(xiàn)手動和自動屏幕旋轉(zhuǎn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識,可以關注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。