最近在修改直播項(xiàng)目出現(xiàn)的問題,需要在直播頁面強(qiáng)制進(jìn)入橫屏,記錄下遇到的坑,稍后會(huì)寫一篇阿里云直播的文章.
-
程序單獨(dú)頁面強(qiáng)制改變方向
(橫豎屏)
在需要改變屏幕方向的類使用這個(gè)方法 (需要設(shè)置DeviceOrientation方向)
CC9E3A21-1E61-48E9-B083-EE9033F200E0.png
if([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) //判斷對(duì)象是否響應(yīng)setOrientation方法
respondsToSelector實(shí)例方法,判斷對(duì)象是否響應(yīng)某個(gè)方法instancesRespondToSelector類方法,判斷類是否響應(yīng)某個(gè)方法
{
SEL selector = NSSelectorFromString(@"setOrientation:");
//NSInvocation對(duì)象只能使用其類方法來初始化衬潦,不可使用alloc/init方法。它執(zhí)行調(diào)用之前疟赊,需要設(shè)置兩個(gè)方法:setSelector: 和setArgument:atIndex:
//創(chuàng)建簽名函數(shù)
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
//設(shè)置selector
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int state = UIInterfaceOrientationLandscapeLeft;//需要設(shè)置的狀態(tài)
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,//垂直,home 鍵在下
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,//垂直,home 鍵在上
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,//水平,home 鍵在右
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft//水平,home 鍵在左
//atIndex的下標(biāo)必須從2開始.原因:0 1 兩個(gè)參數(shù)已經(jīng)被target 和selector占用
[invocation setArgument:&state atIndex:2];
//消息調(diào)用
[invocation invoke];
}```
///因?yàn)椴幌敫膭?dòng)以前的代碼,所以我寫了類別來改變屏幕(此處有坑,如果使用UITabBarController也要給UITabBarController的分類添加方法)
///查了資料蓉冈,只需要自定義 UINavigationController就可以實(shí)現(xiàn)強(qiáng)制轉(zhuǎn)向,寫demo驗(yàn)證的卻如此,但引用到項(xiàng)目不行, 強(qiáng)制轉(zhuǎn)向的類還是會(huì)收到感應(yīng)的影響.排查后發(fā)現(xiàn)是因?yàn)轫?xiàng)目使用了UITabBarController,所以要自定義UITabBarController UINavigationController UIViewController 不想更改項(xiàng)目可寫category
//是否允許轉(zhuǎn)向
-(BOOL)shouldAutorotate{
return YES;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//對(duì)需要定向的類單獨(dú)設(shè)置
if ([self.visibleViewController isKindOfClass:[configViewController class]]) {
return UIInterfaceOrientationMaskLandscapeLeft;//方向設(shè)置
}
return UIInterfaceOrientationMaskPortrait;
}
//如一個(gè)頁面支持多個(gè)方向,這里設(shè)置優(yōu)先方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeLeft;
}
但是這并不能解決的遇到的問題,因?yàn)橹辈ト绻嬅鏅M向的話相機(jī)方向也會(huì)偏向90度并無法調(diào)節(jié),設(shè)置transform屬性旋轉(zhuǎn)90度也會(huì)造成相機(jī)方向偏移,嘗試無果后只好改變思路,橫向布局頁面,橫向布局只需將控件按照方向旋轉(zhuǎn)即可,造成橫屏的假象(如果不使用阿里云直播,使用上面的方法即可)
阿里云直播如果需要做橫豎屏的話,要根據(jù)當(dāng)前手機(jī)的感應(yīng)狀態(tài)來做相應(yīng)的適配,這是很坑的一點(diǎn)
判斷手機(jī)的感應(yīng)方向(偽橫豎屏切換,只需要設(shè)置DeviceOrientation為Portrait)
//監(jiān)聽手機(jī)方向改變
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDeviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
//此處對(duì)相應(yīng)的狀態(tài)做處理
- (void)handleDeviceOrientationDidChange:(UIInterfaceOrientation)interfaceOrientation
{
UIDevice *device = [UIDevice currentDevice] ;
switch (device.orientation) {
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(@"無法辨認(rèn)");
break;
}
}
//銷毀通知
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
以上是我對(duì)橫豎屏切換的一些見解,歡迎大家一起討論.