應(yīng)用場景
目前在做一個(gè)直播類型的APP畦娄,有些界面涉及到橫豎屏切換谐算,再次記錄下其中的技巧熟尉,便于以后查閱,也希望過往的大牛能多指點(diǎn)洲脂,做ios開發(fā)快兩年斤儿,一直在尋找一條突破之路,下面所寫的對(duì)剛進(jìn)入ios的小伙伴們應(yīng)該還是能幫助到恐锦。
項(xiàng)目架構(gòu)
UITabBarController為根控制器管理四個(gè)模塊往果,新建UITabBarController子類,新建UIViewControl的基礎(chǔ)類一铅。
實(shí)現(xiàn)過程
1:在項(xiàng)目中設(shè)置左右Landscape Left 和 Landscape Right?
2:在UITabBarController子類中實(shí)現(xiàn)方法
//是否跟隨屏幕旋轉(zhuǎn)
-(BOOL)shouldAutorotate{
return self.selectedViewController.shouldAutorotate;
}
//支持旋轉(zhuǎn)的方向有哪些
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return [self.selectedViewController supportedInterfaceOrientations];
}
//控制 vc present進(jìn)來的橫豎屏和進(jìn)入方向 陕贮,支持的旋轉(zhuǎn)方向必須包含改返回值的方向
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
3:如果模塊有導(dǎo)航控制器管理,需在新建UINavigationController的子類實(shí)現(xiàn)下面方法:
-(BOOL)shouldAutorotate{
return self.topViewController.shouldAutorotate;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return [self.topViewController supportedInterfaceOrientations];
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
4:因?yàn)槲业捻?xiàng)目應(yīng)用場景就個(gè)別界面需要橫豎屏切換全屏潘飘,所以在baseViewControl中實(shí)現(xiàn)的是不支持旋轉(zhuǎn)切換的方法肮之,然后在需要切換的頁面重現(xiàn)實(shí)現(xiàn)切換方法.
基類中方法:
//是否旋轉(zhuǎn)
-(BOOL)shouldAutorotate{
return NO;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
在需要橫豎屏切換的界面實(shí)現(xiàn):
-(BOOL)shouldAutorotate{
return YES;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
if (_isHalfScreen) { //如果是iPhone,且為豎屏的時(shí)候, 只支持豎屏
return UIInterfaceOrientationMaskPortrait;
}else{
return UIInterfaceOrientationMaskLandscape; //否者只支持橫屏
}
}
- (void)backAction {
[self.navigationController popViewControllerAnimated:YES];
}
具體實(shí)現(xiàn):
我的項(xiàng)目是需要在點(diǎn)擊全屏按鈕后切換為全屏掉缺,也就是手動(dòng)控制橫豎屏,不是橫豎手機(jī)就自動(dòng)全屏(如果需要那樣的話實(shí)現(xiàn)橫豎屏切通知監(jiān)聽戈擒,然后在通知中實(shí)現(xiàn)frame方法就行)眶明。所以在按鈕中的實(shí)現(xiàn)方法為:
#pragma mark -- 全屏點(diǎn)擊事件
- (void)fullAction:(UIButton *)btn {
btn.selected = !btn.selected;
if (btn.selected == NO) {
_isHalfScreen = NO;
[[UIDevice currentDevice]setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait]? forKey:@"orientation"];//這句話是防止手動(dòng)先把設(shè)備置為橫屏,導(dǎo)致下面的語句失效.
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
[UIView animateWithDuration:0.5 animations:^{
playView.frame=self.view.bounds;
} completion:^(BOOL finished) {
}];
}
if (btn.selected == YES) {
_isHalfScreen = YES;
[[UIDevice currentDevice]setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft]? forKey:@"orientation"];//這句話是防止手動(dòng)先把設(shè)備置為橫屏,導(dǎo)致下面的語句失效.
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
[UIView animateWithDuration:0.5 animations:^{
playView.frame = CGRectMake(0, 0, Kwidth, (Kheight-60)/3);
} completion:^(BOOL finished) {
}];
}
}
技巧點(diǎn):
1:狀態(tài)欄顯示設(shè)置:如果顯示狀態(tài)欄的話需在plist文件中將 View controller-based status bar appearance 設(shè)置為NO,然后在application:didFinishLaunchingWithOptions:中添加以下下面代碼
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
2:在播放界面采用masary布局筐高,這樣不需要在切換全屏中重新設(shè)置frame搜囱。