視頻播放支持橫屏

應(yīng)用整體只支持豎屏,只有特定的某個界面支持橫屏

解決方法:

1.在項目中plist文件中設(shè)置支持轉(zhuǎn)屏方向

轉(zhuǎn)屏控制級別: tabar>導(dǎo)航控制器>普通控制器

2.在tabbar/ 導(dǎo)航控制器/ 普通控制器 的.m文件中 復(fù)寫以下三個方法

- (BOOL)shouldAutorotate ; //? 是否支持屏幕自動旋轉(zhuǎn)

-(UIInterfaceOrientationMask)supportedInterfaceOrientations // 支持的轉(zhuǎn)屏方向

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation? // 進(jìn)入后默認(rèn)的屏幕方向(必須包含在支持的屏幕方向里)

1)TabBarVC 中重寫三個方法的代碼如下

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {

return self.selectedViewController.supportedInterfaceOrientations;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

return self.selectedViewController.preferredInterfaceOrientationForPresentation;

}

- (BOOL)shouldAutorotate {

return self.selectedViewController.shouldAutorotate;

}

2)導(dǎo)航控制器基類中重寫三個方法的代碼如下:

- (BOOL)shouldAutorotate{

return self.topViewController.shouldAutorotate;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{

return self.topViewController.supportedInterfaceOrientations;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

return self.topViewController.preferredInterfaceOrientationForPresentation;

}

3)普通控制器基類中代碼如下:

- (BOOL)shouldAutorotate {

return NO;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {

return UIInterfaceOrientationMaskPortrait;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

return UIInterfaceOrientationPortrait;

}

針對沒有tabbar,只有導(dǎo)航控制器的應(yīng)用,可以直接省去TabBarVC中方法重寫代碼;

3.因為項目的大多控制器是不支持自動轉(zhuǎn)屏,且只支持豎屏;因此這些ViewController 繼承自BaseViewController;

針對特定的需要支持 左右橫屏的視頻播放界面,仍需要復(fù)寫以上三個方法

@property(nonatomic,assign)BOOL? ? ? ? ? ? ? ? ? ? autoRotate;

- (BOOL)shouldAutorotate{

return self.autoRotate;}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{

return UIInterfaceOrientationPortrait;}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{

return UIInterfaceOrientationMaskAllButUpsideDown;}

旋轉(zhuǎn)屏幕

- (void)switchToLandscapePotrait {

_autoRotate = YES;

//? 如果當(dāng)前設(shè)備是物理橫屏,先恢復(fù)為豎屏,保證后面有轉(zhuǎn)屏動畫

if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];

}

// 手動設(shè)置橫屏,會調(diào)用 方法 shouldAutorotate

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];

[UIApplication sharedApplication].statusBarHidden = YES;

?_autoRotate = NO;

}

代碼手動設(shè)置橫屏, ? [ [UIDevice currentDevice] setValue: forKey: ]會調(diào)用? 方法- (BOOL)shouldAutorotate,如果該方法返回的是NO,則無法使用代碼設(shè)置橫屏;

所有需要在調(diào)用前 設(shè)置_autoRotate = YES;? 調(diào)用完畢設(shè)置_autoRotate = YES;?

注意事項:?

當(dāng)手機(jī)橫放,已經(jīng)是物理橫屏的時候,再手動設(shè)置橫屏是無效的;所以此處需要做處理(如果是物理橫屏,先恢復(fù)為物理豎屏)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容