一般直播間 / 視頻為了滿足用戶的需要置侍,都會有小窗播放和全屏播放,下面我說說我的實現(xiàn)方案
一
- 點擊全屏按鈕時桅打,可以通過改變視圖的
transform
及大小來達到橫屏的效果豌习,但是這樣會有幾個問題,一是狀態(tài)欄沒有轉(zhuǎn)成橫屏银萍;二是不能感應(yīng)手機自己的旋轉(zhuǎn)变勇;三是當有鍵盤彈出時,還是豎屏樣式。 所以此方案不是很OK搀绣。
二
target屏幕支持方向勾選
- 一般的項目默認
Device Orientation
支持Portrait
飞袋,如果需要某些界面支持橫屏可以勾選圖上的left 或者 right - 在基類控制器需要重寫兩個方法,防止所有控制器都支持旋轉(zhuǎn)橫豎屏链患。
// 是否支持設(shè)備自動旋轉(zhuǎn)
- (BOOL)shouldAutorotate {
return NO;
}
// 支持屏幕顯示方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- 在需要橫豎屏的控制器重寫以下方法巧鸭,即可響應(yīng)手機的旋轉(zhuǎn)動作,并且旋轉(zhuǎn)麻捻。
// 支持設(shè)備自動旋轉(zhuǎn)
- (BOOL)shouldAutorotate {
return YES;
}
// 支持橫屏顯示
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
// 如果該界面需要支持橫豎屏切換
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
}
- 如果不能旋轉(zhuǎn)纲仍,那么需要查看當前控制器是否有父 NavigationController / TabbarController管理,如果有贸毕,則需要在父 NavigationController / TabbarController 重寫以下方法支持旋轉(zhuǎn)才可以(或者分類)郑叠。
- (BOOL)shouldAutorotate {
// 返回當前子控制器 是否支持旋轉(zhuǎn)
return [self.topViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [self.topViewController supportedInterfaceOrientations];
}
- 在切換橫豎屏之后,需要對部分子視圖更新布局崖咨,所以需要監(jiān)聽設(shè)備方向的變化并且做出響應(yīng)锻拘。
// 在控制器中添加通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
- (void)deviceOrientationDidChange {
// 當前是豎屏狀態(tài)
if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
[self orientationChange:NO];
//注意: UIDeviceOrientationLandscapeLeft 與 UIInterfaceOrientationLandscapeRight
} else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
[self orientationChange:YES];
}
}
- (void)orientationChange:(BOOL)landscapeRight {
if(landscapeRight) {
// 切換到橫屏之后 子視圖需要修改的操作
} else {
// 切換到豎屏之后 子視圖需要修改的操作
}
}
- 手動點擊切換橫豎屏 需要調(diào)用
setOrientation:
油吭,該方法在iOS3.0后變成私有方法击蹲,我們可以通過NSInvocation去調(diào)用
- (void)fullScreenButtonClick {
//屏幕橫屏的方法
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
// UIInterfaceOrientationPortrait 豎屏的參數(shù)
int val = UIInterfaceOrientationLandscapeRight;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}