1. 使用viewWillTransitionToSize:withTransitionCoordinator: 監(jiān)聽屏幕旋轉:
iOS9之前,最常用的監(jiān)聽函數(shù)是如下兩個:
//轉屏前回調函數(shù)
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)durationNS_DEPRECATED_IOS(3_0,8_0,"Implement viewWillTransitionToSize:withTransitionCoordinator: instead")__TVOS_PROHIBITED;
//轉屏后回調函數(shù)
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientationNS_DEPRECATED_IOS(2_0,8_0)__TVOS_PROHIBITED;
但這兩個協(xié)議方法已經(jīng)在iOS9之后廢棄了(雖然也可以繼續(xù)調用,但官方不建議使用了)
文檔提示,改用如下函數(shù):
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator;
轉屏前后的監(jiān)聽如下:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context){
NSLog(@"轉屏前調入");
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context){
NSLog(@"轉屏后調入");
}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
但這個監(jiān)聽方法有個局限性,這個協(xié)議方法來自于<UIContentContainer>腹缩,而查看官方代碼可知,只有UIViewController及其子類,才能遵循并實現(xiàn)這個協(xié)議方法如绸。即只有UIViewController才能夠使用這個方法監(jiān)聽到 (且UIViewController是以一個類實例存在的)。
2. 通過 UIDeviceOrientationDidChangeNotification 通知監(jiān)聽:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//開始生成 設備旋轉 通知
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
//添加 設備旋轉 通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
//銷毀 設備旋轉 通知
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIDeviceOrientationDidChangeNotification
object:nil
];
//結束 設備旋轉通知
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}
/**屏幕旋轉的通知回調*/
- (void)orientChange:(NSNotification *)noti {
UIDeviceOrientation orient = [UIDevice currentDevice].orientation;
switch (orient) {
case UIDeviceOrientationPortrait:
NSLog(@"豎直屏幕");
break;
case UIDeviceOrientationLandscapeLeft:
NSLog(@"手機左轉");
break;
case UIDeviceOrientationPortraitUpsideDown:
NSLog(@"手機豎直");
break;
case UIDeviceOrientationLandscapeRight:
NSLog(@"手機右轉");
break;
case UIDeviceOrientationUnknown:
NSLog(@"未知");
break;
case UIDeviceOrientationFaceUp:
NSLog(@"手機屏幕朝上");
break;
case UIDeviceOrientationFaceDown:
NSLog(@"手機屏幕朝下");
break;
default:
break;
}
}
這里監(jiān)聽的是屏幕方向旭贬。UIDeviceOrientation除了橫豎屏4個方向以外怔接,還包括“屏幕朝上”、“屏幕朝下”這兩個方向稀轨,處理業(yè)務邏輯時需要注意著兩個方向的特殊性扼脐。
因為通知的便利性,所以監(jiān)聽不需要局限于UIViewController里奋刽,而是任意位置瓦侮。但注意要及時移除掉通知。
3. 通過 UIApplicationDidChangeStatusBarOrientationNotification 通知監(jiān)聽:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//添加 設備旋轉 通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
//銷毀 設備旋轉 通知
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
}
/**屏幕旋轉的通知回調*/
- (void)orientChange:(NSNotification *)noti {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
switch (orientation) {
case UIInterfaceOrientationUnknown:
NSLog(@"未知");
break;
case UIInterfaceOrientationPortrait:
NSLog(@"豎直");
break;
case UIInterfaceOrientationPortraitUpsideDown:
NSLog(@"屏幕倒立");
break;
case UIInterfaceOrientationLandscapeLeft:
NSLog(@"手機水平佣谐,home鍵在左邊");
break;
case UIInterfaceOrientationLandscapeRight:
NSLog(@"手機水平肚吏,home鍵在右邊");
break;
default:
break;
}
}
整體同理于方法2,但有一些區(qū)別狭魂。獲取到的方向是UIInterfaceOrientation罚攀,即界面方向党觅,只有“上下左右”4個方向。
PS:[UIApplication sharedApplication].statusBarOrientation在iOS13之后會被廢棄掉坞生,官方建議使用window的方向仔役。即如下:
[UIApplication sharedApplication].windows.firstObject.windowScene.interfaceOrientation
但這個API也只能在iOS13之后才能使用。
參考文檔:
【1】IOS9開始如何處理轉屏