iOS開發(fā)中控制屏幕旋轉(zhuǎn)的編寫方法小結(jié)
在iOS5.1 和 之前的版本中, 我們通常利用 shouldAutorotateToInterfaceOrientation: 來單獨(dú)控制某個UIViewController的旋屏方向支持,比如:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
但是在iOS6中,這個方法被廢棄了,使用無效。
shouldAutorotateToInterfaceOrientation:
Returns a Boolean value indicating whether the view controller supports the specified orientation. (Deprecated in iOS 6.0. Override the supportedInterfaceOrientations andpreferredInterfaceOrientationForPresentation methods instead.)
實(shí)踐后會發(fā)現(xiàn),通過supportedInterfaceOrientations的單獨(dú)控制是無法鎖定屏幕的。
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
多次實(shí)驗(yàn)后總結(jié)出控制屏幕旋轉(zhuǎn)支持方向的方法如下:
子類化UINavigationController,增加方法
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
并且設(shè)定其為程序入口,或指定為 self.window.rootViewController
隨后添加自己的view controller,如果想禁止某個view controller的旋屏:(支持全部版本的控制)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
如果想又開啟某個view controller的全部方向旋屏支持:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate
{
return YES;
}
從而實(shí)現(xiàn)了對每個view controller的單獨(dú)控制。
順便提一下,如果整個應(yīng)用所有view controller都不支持旋屏,那么干脆:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}
橫豎屏切換,視圖亂了怎么辦?
首先,我們必須了解一下下列4種狀態(tài),它們被用來描述設(shè)備旋轉(zhuǎn)方向:
對于旋屏的處理,大致分為如下幾種情況和思路:
也許,你不需要旋屏支持,而希望鎖定屏幕
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
也許,你需要支持旋屏,或者支持部分方向的旋屏
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
也許,你的view有張背景圖,旋屏?xí)r系統(tǒng)幫助你拉伸了圖片,但是卻沒有管你的其它部件,比如button,你希望直接改變button的大小和位置
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
NSLog(@"現(xiàn)在是豎屏");
[btn setFrame:CGRectMake(213, 442, 340, 46)];
}
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
NSLog(@"現(xiàn)在是橫屏");
[btn setFrame:CGRectMake(280, 322, 460, 35)];
}
}
也許,你并不希望用絕對坐標(biāo)去約束控件,而是希望讓它通過旋轉(zhuǎn)自己適應(yīng)屏幕的旋轉(zhuǎn)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIDevice *device = [UIDevice currentDevice];
[device beginGeneratingDeviceOrientationNotifications];
//利用 NSNotificationCenter 獲得旋轉(zhuǎn)信號 UIDeviceOrientationDidChangeNotification
NSNotificationCenter *ncenter = [NSNotificationCenter defaultCenter];
[ncenter addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:device];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(void)rotation_btn:(float)n
{
UIButton *robtn = self.btn;
robtn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
}
-(void)orientationChanged
{
UIDeviceOrientation orientaiton = [[UIDevice currentDevice] orientation];
switch (orientaiton) {
caseUIDeviceOrientationPortrait:
[self rotation_btn:0.0];
break;
caseUIDeviceOrientationPortraitUpsideDown:
[self rotation_btn:90.0*2];
break;
caseUIDeviceOrientationLandscapeLeft:
[self rotation_btn:90.0*3];
break;
caseUIDeviceOrientationLandscapeRight:
[self rotation_btn:90.0];
break;
default:
break;
}
}
也許,你需要autoresizesSubviews = YES
也許,你希望橫豎屏有不同的布局效果,需要準(zhǔn)備2份Subview,在不同狀態(tài)去替換
當(dāng)然不要忘記,需要調(diào)節(jié)設(shè)定圖示中的1、2處,
來幫助我們完成自己想要的適應(yīng)效果。Example 動畫呈現(xiàn)的很清晰,^_^ 我就不再啰嗦了。
相關(guān)文章
iOS使用UICountingLabel實(shí)現(xiàn)數(shù)字變化的動畫效果
本文主要介紹了iOS使用UICountingLabel實(shí)現(xiàn)數(shù)字變化動畫效果的方法,具有一定的參考價值,下面跟著小編一起來看下吧2016-12-12iOS開發(fā)中使用UIScrollView實(shí)現(xiàn)無限循環(huán)的圖片瀏覽器
這篇文章主要介紹了iOS開發(fā)中使用UIScrollView實(shí)現(xiàn)無限循環(huán)的圖片瀏覽器的方法,感興趣的小伙伴們可以參考一下2016-03-03iOS中在APP內(nèi)加入AppStore評分功能的實(shí)現(xiàn)方法
這篇文章主要介紹了iOS中在APP內(nèi)加入AppStore評分功能的實(shí)現(xiàn)方法,文中筆者給大家整理了三種方式,大家可以根據(jù)自己的需求選擇,需要的朋友可以參考下2017-11-11iOS Webview自適應(yīng)實(shí)際內(nèi)容高度的4種方法詳解
這篇文章主要介紹了iOS Webview自適應(yīng)實(shí)際內(nèi)容高度的4種方法詳解,本文介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友可以參考下2016-09-09iOS的UIColor類與其相關(guān)類之間的區(qū)別及判斷相等的方法
這篇文章主要介紹了iOS的UIColor類與其相關(guān)類之間的區(qū)別及判斷相等的方法,主要是對比了CGColor和CIColor,需要的朋友可以參考下2015-10-10