我現(xiàn)在用到的橫豎屏比較簡單闲昭,等以后遇到復雜的再來更新吧
最簡單的橫豎屏
1栖榨、勾選可旋轉方向
根據(jù)需求,自己勾選旋轉方向
此時坟奥,便可以隨便旋轉了树瞭,只是我們還需要更新布局
2、在我們需要更新布局的view中爱谁,重寫方法
// 更新界面布局
override func layoutSubviews() {
[super layoutSubviews];
//通過狀態(tài)欄電池圖標判斷橫豎屏
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationMaskPortrait) {
//豎屏布局
} else {
//橫屏布局
}
}
3晒喷、控制器中如果要更新布局,需調用方法
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
// 屏幕旋轉調用
if (size.width > size.height) {
//橫屏設置
}else{
//豎屏設置
}
}
這里访敌,最簡單的橫豎屏設置完成
基礎知識
三種枚舉:UIDeviceOrientation凉敲、UIInterfaceOrientation、UIInterfaceOrientationMask
1、設備方向:UIDeviceOrientation
UIDeviceOrientation是指設備iPhone或者iPad本身旋轉的方向荡陷,設備方向包括7中雨效,其中有一種是未知方向,旋轉方向是根據(jù)home鍵來判斷的废赞,源碼如下
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
} __TVOS_PROHIBITED;
監(jiān)聽當前設備旋轉的方法:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onDeviceOrientationDidChange)
name:UIDeviceOrientationDidChangeNotification
object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
- (BOOL)onDeviceOrientationDidChange{
//獲取當前設備Device
UIDevice *device = [UIDevice currentDevice] ;
//識別當前設備的旋轉方向
switch (device.orientation) {
case UIDeviceOrientationFaceUp:
NSLog(@"屏幕向上平躺");
break;
case UIDeviceOrientationFaceDown:
NSLog(@"屏幕向下平躺");
break;
case UIDeviceOrientationUnknown:
//系統(tǒng)當前無法識別設備朝向,可能是傾斜
NSLog(@"未知方向");
break;
case UIDeviceOrientationLandscapeLeft:
NSLog(@"屏幕向左橫置");
break;
case UIDeviceOrientationLandscapeRight:
NSLog(@"屏幕向右橫置");
break;
case UIDeviceOrientationPortrait:
NSLog(@"屏幕直立");
break;
case UIDeviceOrientationPortraitUpsideDown:
NSLog(@"屏幕直立叮姑,上下顛倒");
break;
default:
NSLog(@"無法識別");
break;
}
return YES;
}
注意:設備方向智能獲取唉地,不能更改
2、界面方向:UIInterfaceOrientation
界面方向可以自己設置
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
} __TVOS_PROHIBITED;
3传透、界面方向: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;
我覺得耘沼,這個就是多了兩種枚舉,讓我們在使用的時候更方便一些朱盐,別的群嗤,我也沒看出來啥???♀?
當我們在某一個界面,需要單獨設置旋轉方向的時候兵琳,就使用以下方法
override var shouldAutorotate: Bool {
// 支持界面自動旋轉狂秘,返回true,否則返回false
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
// 支持的旋轉方向
return UIInterfaceOrientationMask.all
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
// 進入界面設置默認顯示方向
return UIInterfaceOrientation.portrait
}