iOS開(kāi)發(fā)中控制屏幕旋轉(zhuǎn)的編寫(xiě)方法小結(jié)
在iOS5.1 和 之前的版本中, 我們通常利用 shouldAutorotateToInterfaceOrientation: 來(lái)單獨(dú)控制某個(gè)UIViewController的旋屏方向支持,比如:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
但是在iOS6中,這個(gè)方法被廢棄了,使用無(wú)效。
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í)踐后會(huì)發(fā)現(xiàn),通過(guò)supportedInterfaceOrientations的單獨(dú)控制是無(wú)法鎖定屏幕的。
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
多次實(shí)驗(yàn)后總結(jié)出控制屏幕旋轉(zhuǎn)支持方向的方法如下:
子類(lèi)化UINavigationController,增加方法
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
并且設(shè)定其為程序入口,或指定為 self.window.rootViewController
隨后添加自己的view controller,如果想禁止某個(gè)view controller的旋屏:(支持全部版本的控制)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
如果想又開(kāi)啟某個(gè)view controller的全部方向旋屏支持:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate
{
return YES;
}
從而實(shí)現(xiàn)了對(duì)每個(gè)view controller的單獨(dú)控制。
順便提一下,如果整個(gè)應(yīng)用所有view controller都不支持旋屏,那么干脆:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}
橫豎屏切換,視圖亂了怎么辦?
首先,我們必須了解一下下列4種狀態(tài),它們被用來(lái)描述設(shè)備旋轉(zhuǎn)方向:
對(duì)于旋屏的處理,大致分為如下幾種情況和思路:
也許,你不需要旋屏支持,而希望鎖定屏幕
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
也許,你需要支持旋屏,或者支持部分方向的旋屏
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
也許,你的view有張背景圖,旋屏?xí)r系統(tǒng)幫助你拉伸了圖片,但是卻沒(méi)有管你的其它部件,比如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)];
}
}
也許,你并不希望用絕對(duì)坐標(biāo)去約束控件,而是希望讓它通過(guò)旋轉(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)信號(hào) 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處,
來(lái)幫助我們完成自己想要的適應(yīng)效果。Example 動(dòng)畫(huà)呈現(xiàn)的很清晰,^_^ 我就不再啰嗦了。
相關(guān)文章
iOS通過(guò)攝像頭圖像識(shí)別技術(shù)分享
本篇文章給大家詳細(xì)講述了讓IOS開(kāi)發(fā)中通過(guò)攝像頭進(jìn)行圖像識(shí)別的相關(guān)技術(shù),對(duì)此有興趣的朋友參考學(xué)習(xí)下吧。2018-02-02iOS使用UICountingLabel實(shí)現(xiàn)數(shù)字變化的動(dòng)畫(huà)效果
本文主要介紹了iOS使用UICountingLabel實(shí)現(xiàn)數(shù)字變化動(dòng)畫(huà)效果的方法,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2016-12-12iOS拍照后圖片自動(dòng)旋轉(zhuǎn)90度的完美解決方法
今天開(kāi)發(fā)一個(gè)拍照獲取照片的功能的時(shí)候, 發(fā)現(xiàn)上傳之后圖片會(huì)自動(dòng)旋轉(zhuǎn)90.在測(cè)試中發(fā)現(xiàn)只要是圖片大于2M, 系統(tǒng)就會(huì)自動(dòng)翻轉(zhuǎn)照片。下面小編通過(guò)本文給大家分享下解決辦法2016-12-12iOS開(kāi)發(fā)中使用UIScrollView實(shí)現(xiàn)無(wú)限循環(huán)的圖片瀏覽器
這篇文章主要介紹了iOS開(kāi)發(fā)中使用UIScrollView實(shí)現(xiàn)無(wú)限循環(huán)的圖片瀏覽器的方法,感興趣的小伙伴們可以參考一下2016-03-03iOS中在APP內(nèi)加入AppStore評(píng)分功能的實(shí)現(xiàn)方法
這篇文章主要介紹了iOS中在APP內(nèi)加入AppStore評(píng)分功能的實(shí)現(xiàn)方法,文中筆者給大家整理了三種方式,大家可以根據(jù)自己的需求選擇,需要的朋友可以參考下2017-11-11iOS判斷用戶(hù)是否打開(kāi)APP通知開(kāi)關(guān)
這篇文章主要為大家詳細(xì)介紹了iOS判斷用戶(hù)是否打開(kāi)APP通知開(kāi)關(guān)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04iOS Webview自適應(yīng)實(shí)際內(nèi)容高度的4種方法詳解
這篇文章主要介紹了iOS Webview自適應(yīng)實(shí)際內(nèi)容高度的4種方法詳解,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09iOS的UIColor類(lèi)與其相關(guān)類(lèi)之間的區(qū)別及判斷相等的方法
這篇文章主要介紹了iOS的UIColor類(lèi)與其相關(guān)類(lèi)之間的區(qū)別及判斷相等的方法,主要是對(duì)比了CGColor和CIColor,需要的朋友可以參考下2015-10-10