一、屏幕旋轉(zhuǎn)方向監(jiān)聽(tīng)
1、UIDeviceOrientation:設(shè)備方向
iOS 定義了七種設(shè)備方向:
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
UIDeviceOrientationUnknown, // 未知方向,可能是設(shè)備(屏幕)斜置
UIDeviceOrientationPortrait, // 設(shè)備(屏幕)直立
UIDeviceOrientationPortraitUpsideDown, // 設(shè)備(屏幕)直立,上下顛倒
UIDeviceOrientationLandscapeLeft, // 設(shè)備(屏幕)向左橫置
UIDeviceOrientationLandscapeRight, // 設(shè)備(屏幕)向右橫置
UIDeviceOrientationFaceUp, // 設(shè)備(屏幕)朝上平躺
UIDeviceOrientationFaceDown // 設(shè)備(屏幕)朝下平躺
};
//注意:UIDeviceOrientation參考home鍵方向,如:home方向在右,設(shè)備(屏幕)方向向左(UIDeviceOrientationLandscapeLeft)
當(dāng)設(shè)備方向變化時(shí)候,發(fā)出UIDeviceOrientationDidChangeNotification通知;注冊(cè)監(jiān)聽(tīng)該通知,可以針不同的設(shè)備方向處理視圖展示。
注:手機(jī)鎖定豎屏后,UIDeviceOrientationDidChangeNotification通知就失效了。
代碼如下:
//開(kāi)啟和監(jiān)聽(tīng) 設(shè)備旋轉(zhuǎn)的通知(不開(kāi)啟的話,設(shè)備方向一直是UIInterfaceOrientationUnknown)
if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleDeviceOrientationChange:)
name:UIDeviceOrientationDidChangeNotification object:nil];
//設(shè)備方向改變的處理
- (void)handleDeviceOrientationChange:(NSNotification *)notification{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
switch (ddeviceOrientation) {
case UIDeviceOrientationFaceUp:
NSLog(@"屏幕朝上平躺");
break;
case UIDeviceOrientationFaceDown:
NSLog(@"屏幕朝下平躺");
break;
case UIDeviceOrientationUnknown:
NSLog(@"未知方向");
break;
case UIDeviceOrientationLandscapeLeft:
NSLog(@"屏幕向左橫置");
break;
case UIDeviceOrientationLandscapeRight:
NSLog(@"屏幕向右橫置");
break;
case UIDeviceOrientationPortrait:
NSLog(@"屏幕直立");
break;
case UIDeviceOrientationPortraitUpsideDown:
NSLog(@"屏幕直立,上下顛倒");
break;
default:
NSLog(@"無(wú)法辨識(shí)");
break;
}
}
//最后在dealloc中移除通知 和結(jié)束設(shè)備旋轉(zhuǎn)的通知
- (void)dealloc{
//...
[[NSNotificationCenter defaultCenter]removeObserver:self];
[[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
}
2、UIInterfaceOrientation:界面方向
iOS 定義了五種界面方向
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown, //未知方向
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait, //界面直立
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, //界面直立,上下顛倒
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight, //界面朝左
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft //界面朝右
} __TVOS_PROHIBITED;
注:界面方向和設(shè)別方向有對(duì)應(yīng)關(guān)系,如界面的豎直方向就是 設(shè)備的豎直方向:UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown
要想界面旋轉(zhuǎn),
在如圖所示選擇支持的界面旋轉(zhuǎn)方向:

同時(shí)在
UIViewController中加入以下兩個(gè)方法:
///是否支持自動(dòng)轉(zhuǎn)屏
-(BOOL)shouldAutorotate
{
return YES;///很明顯如果返回YES就可以,返回NO就不行
}
///如果上面方法返回YES則會(huì)根據(jù)這個(gè)方法判斷支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;///這個(gè)是所有方向
}
當(dāng)界面方向變化時(shí)候,先后發(fā)出 UIApplicationWillChangeStatusBarOrientationNotification和 UIApplicationDidChangeStatusBarOrientationNotification通知,手機(jī)鎖定豎屏后,這兩個(gè)通知也失效了;注冊(cè)監(jiān)聽(tīng)這兩個(gè)通知,可以針對(duì)不同的界面方向處理視圖展示。
代碼如下所示:
//以監(jiān)聽(tīng)UIApplicationDidChangeStatusBarOrientationNotification通知為例
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleStatusBarOrientationChange:)
name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
//界面方向改變的處理
- (void)handleStatusBarOrientationChange: (NSNotification *)notification{
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
switch (interfaceOrientation) {
case UIInterfaceOrientationUnknown:
NSLog(@"未知方向");
break;
case UIInterfaceOrientationPortrait:
NSLog(@"界面直立");
break;
case UIInterfaceOrientationPortraitUpsideDown:
NSLog(@"界面直立,上下顛倒");
break;
case UIInterfaceOrientationLandscapeLeft:
NSLog(@"界面朝左");
break;
case UIInterfaceOrientationLandscapeRight:
NSLog(@"界面朝右");
break;
default:
break;
}
}
//最后在dealloc中移除通知
- (void)dealloc{
//...
[[NSNotificationCenter defaultCenter]removeObserver:self];
[[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
}
3、UIInterfaceOrientationMask
UIInterfaceOrientationMask是為了集成多種UIInterfaceOrientation而定義的類型,和UIViewController相關(guān),一共有7種
iOS中的UIInterfaceOrientationMask定義
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),
} __TVOS_PROHIBITED;
在ViewController可以重寫- (UIInterfaceOrientationMask)supportedInterfaceOrientations方法返回類型,來(lái)決定UIViewController可以支持哪些界面方向。
//支持界面直立
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
二、視圖控制器中旋轉(zhuǎn)方向的設(shè)置
1、禁止橫屏操作
- 方法一:在項(xiàng)目的General-->Deployment Info-->Device Orientation中,只勾選Portrait(豎屏)
- 方法二:
Device Orientation默認(rèn)設(shè)置,在Appdelegate中實(shí)現(xiàn)supportedInterfaceOrientationsForWindow:只返回UIInterfaceOrientationMaskPortraitt(豎屏)
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskPortrait;
}
參考鏈接:http://www.cocoachina.com/ios/20170711/19808.html
項(xiàng)目Demo托管在[GitHub][]上
[GitHub]:https://github.com/lizishiye/DeviceOrientation_Demo