IOS横竖屏控制与事件处理
生活随笔
收集整理的這篇文章主要介紹了
IOS横竖屏控制与事件处理
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
公司App里面有個(gè)需求,即所有界面都是豎屏,且不允許橫屏切換,唯獨(dú)有一個(gè)圖表界面允許橫屏。那么,根據(jù)此需求處理如下:
首先,確保App本身應(yīng)該允許轉(zhuǎn)屏切換:
再次,我的App里面都是走UINavigationController進(jìn)行界面push切換的,所以首先創(chuàng)建一個(gè)UINavigationController的子類,并設(shè)定允許轉(zhuǎn)屏:
1 @implementation AppExtendNavigationController 2 - (void)viewDidLoad { 3 [super viewDidLoad]; 4 // Do any additional setup after loading the view. 5 } 6 - (void)didReceiveMemoryWarning { 7 [super didReceiveMemoryWarning]; 8 // Dispose of any resources that can be recreated. 9 } 10 #pragma mark 轉(zhuǎn)屏方法重寫(xiě) 11 -(UIInterfaceOrientationMask)supportedInterfaceOrientations 12 { 13 return [self.viewControllers.lastObject supportedInterfaceOrientations]; 14 } 15 -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 16 { 17 return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation]; 18 } 19 -(BOOL)shouldAutorotate{ 20 return self.visibleViewController.shouldAutorotate; 21 }最后,在你不想轉(zhuǎn)屏切換的ViewController上重寫(xiě)以下方法:
1 #pragma mark 轉(zhuǎn)屏方法 不允許轉(zhuǎn)屏 2 -(UIInterfaceOrientationMask)supportedInterfaceOrientations 3 { 4 return UIInterfaceOrientationMaskPortrait ; 5 } 6 - (BOOL)shouldAutorotate 7 { 8 return NO; 9 } 10 -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 11 { 12 return UIInterfaceOrientationPortrait; 13 } 14 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ 15 return NO; 16 }?
在你想轉(zhuǎn)屏切換的ViewController上可以照這樣重寫(xiě)(允許左右橫屏以及豎屏):
1 - (BOOL)shouldAutorotate { 2 return YES; 3 } 4 -(UIInterfaceOrientationMask)supportedInterfaceOrientations 5 { 6 return UIInterfaceOrientationMaskAll; 7 } 8 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 9 { 10 return UIInterfaceOrientationPortrait; 11 } 12 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 13 { 14 return YES; 15 }?
另外,在ViewController中對(duì)于轉(zhuǎn)屏事件可以參見(jiàn)下面的方法進(jìn)行捕獲:
1 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator 2 { 3 [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; 4 [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) { 5 //計(jì)算旋轉(zhuǎn)之后的寬度并賦值 6 CGSize screen = [UIScreen mainScreen].bounds.size; 7 //界面處理邏輯 8 self.lineChartView.frame = CGRectMake(0, 30, screen.width, 200.0); 9 //動(dòng)畫(huà)播放完成之后 10 if(screen.width > screen.height){ 11 NSLog(@"橫屏"); 12 }else{ 13 NSLog(@"豎屏"); 14 } 15 } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) { 16 NSLog(@"動(dòng)畫(huà)播放完之后處理"); 17 }]; 18 }?
區(qū)分當(dāng)前屏幕是否為橫豎屏的狀態(tài),其實(shí)通過(guò)判斷當(dāng)前屏幕的寬高來(lái)決定是不是橫屏或者豎屏:
豎屏?xí)r:寬<高
橫屏?xí)r:寬>高
以上在IOS8、9中測(cè)試通過(guò)
?
轉(zhuǎn)載于:https://www.cnblogs.com/fengmin/p/5377246.html
總結(jié)
以上是生活随笔為你收集整理的IOS横竖屏控制与事件处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: cocos2dx的图片载入
- 下一篇: sql 获取本周周一和周日