由于項(xiàng)目的需要,在播放視頻的ViewController中需要支持橫屏旋轉(zhuǎn)播放器,故重新寫了一下整個(gè)app的控制。
開始吧,首先保證一下自己的項(xiàng)目是支持三個(gè)方向的旋轉(zhuǎn)的。
這里明確一下,相信大家做項(xiàng)目的時(shí)候都已經(jīng)有一種意識(shí),就是在開始項(xiàng)目的時(shí)候,都配備了 BaseViewController、BaseNavigationController 這兩個(gè)最基礎(chǔ)的類以供所有的ViewController,NavigationController繼承。
額:如果沒有,建議盡快添加 :-D
只要項(xiàng)目支持三個(gè)方向的旋轉(zhuǎn),不添加任何代碼的情況下,app在手機(jī)旋轉(zhuǎn)的時(shí)候就會(huì)自動(dòng)旋轉(zhuǎn)的。
在BaseNavigationController增加下面兩個(gè)方法:
- (BOOL)shouldAutorotate {
if (self.topViewController != nil) {
//如果當(dāng)前有VC,獲取VC里面的shouldAutorotate
return [self.topViewController shouldAutorotate];
}
return [super shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
if (self.topViewController != nil) {
//如果當(dāng)前有VC,獲取VC里面的supportedInterfaceOrientations
return [self.topViewController supportedInterfaceOrientations];
}
return [super supportedInterfaceOrientations];
}
在BaseViewController增加下面兩個(gè)方法:
- (BOOL)shouldAutorotate {
//這里需要返回YES
// 如果你的項(xiàng)目僅僅在某個(gè)VC里面支持橫屏,其他都不支持的話,這里也需要返回YES
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//如果你的項(xiàng)目僅僅在某個(gè)VC里面支持橫屏,其他都不支持的話,這里也需要返回UIInterfaceOrientationMaskPortrait
//而如果你的項(xiàng)目每一個(gè)頁面都需要支持橫屏,這里返回UIInterfaceOrientationMaskAllButUpsideDown就可以了
return UIInterfaceOrientationMaskPortrait;
}
現(xiàn)在的代碼代表是項(xiàng)目所有頁面都只支持一個(gè)方向Portrait,不支持橫屏。
如果你現(xiàn)在新建一個(gè)需要橫屏的LandscapeViewController,則你只需要在 LandscapeViewController.h 繼承BaseViewController
@interface LandscapeViewController : BaseViewController
在LandscapeViewController.m 里面 增加
#pragma mark 開放橫屏切換
- (BOOL)shouldAutorotate {
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
現(xiàn)在就實(shí)現(xiàn)了在整個(gè)項(xiàng)目中 僅僅LandscapeViewController這個(gè)VC能旋轉(zhuǎn)。