一、在應用中從豎屏模式強制轉換為橫屏模式
-
第一種方法:通過模態彈出視圖的方式,使得特定ViewController堅持特定的interfaceOrientation
(1)iOS6之后提供了這樣一個方法,可以讓你的Controller倔強的堅持某個特定的interfaceOrientation:
-
(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0)
{
return UIInterfaceOrientationLandscapeRight;//左下右上顯示這里有5種供選擇,具體什么方向顯示,自己可以試一下
//UIInterfaceOrientationUnknown
//UIInterfaceOrientationPortrait
//UIInterfaceOrientationPortraitUpsideDown
//UIInterfaceOrientationLandscapeLeft
//UIInterfaceOrientationLandscapeRight
}
-
(2)當然,使用這個方法是有前提的,就是當前ViewController是通過全屏的Presentation(模態視圖)方式展現出來的。而且需要設置下面方法返回值為NO,這樣控制器就不會再進行旋轉,而是以你設定方向顯示。
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0)
{
return NO;
}
-
第二種方法:通過人為的辦法改變view.transform的屬性。
具體辦法:view.transform一般是View的旋轉,拉伸移動等屬性,類似view.layer.transform,區別在于View.transform是二維的,也就是使用仿射的辦法通常就是帶有前綴CGAffineTransform的類(可以到API文檔里面搜索這個前綴的所有類),而view.layer.transform可以在3D模式下面的變化,通常使用的都是前綴為CATransform3D的類。
這里要記住一點,當你改變過一個view.transform屬性或者view.layer.transform的時候需要恢復默認狀態的話,記得先把他們重置可以使用view.transform = CGAffineTransformIdentity,或者view.layer.transform = CATransform3DIdentity,假設你一直不斷的改變一個view.transform的屬性,而每次改變之前沒有重置的話,你會發現后來的改變和你想要的發生變化了,不是你真正想要的結果。
好了,上面介紹了旋轉的屬性,接下來就是關鍵了。官方提供了一個辦法就是查看當前電池條的狀態UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;通過這個辦法,你可以知道當前屏幕的電池條的顯示方向,而且你還可以強制設置他的顯示方向,通過設置這個屬性就OK了,可以選擇是否動畫改變電池條方向。有了這兩個那我們就可以任意的改變我們想要的顯示方式了。
(1)獲取當前電池條的方向UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation (2)獲取當前屏幕的大小CGRect frame = [UIScreen mainScreen].applicationFrame; (3)設置我們的View的中心點 CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2)); (4)根據當前電池條的方向,獲取需要旋轉的角度的大小。通常 - (CGAffineTransform)getARotation { if (orientation == UIInterfaceOrientationLandscapeLeft) { return CGAffineTransformMakeRotation(M_PI*1.5); } else if (orientation == UIInterfaceOrientationLandscapeRight) { return CGAffineTransformMakeRotation(M_PI/2); } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { return CGAffineTransformMakeRotation(-M_PI); } else { return CGAffineTransformIdentity; } } (5)可以動畫的改變我們view的顯示方式了 [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:YES]; CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;(獲取當前電池條動畫改變的時間) [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:duration]; //在這里設置view.transform需要匹配的旋轉角度的大小就可以了。 //把(4)中返回的rotation賦給self.view.transform self.view.transform = [self getARotation]; [UIView commitAnimations];
-
第三種方法:通過setOrientation:的辦法強制性的旋轉到一個特定的方向。
注意:Apple在3.0以后都不支持這個辦法了,這個辦法已經成為了私有的了,但是要跳過App Stroe的審核,需要一點巧妙的辦法。
不要直接調用[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationLandscapeRight]這樣的辦法來強制性的橫屏,這樣導致你的程序是很難通過App Store審核的。但是你可以選擇使用performSelector的辦法來調用它。具體就幾行代碼如下: //強制橫屏 if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationLandscapeRight]; }
總結:如果第一種辦法可以滿足你需要的話,最好使用第一種辦法,因為第一種方法在App Store肯定沒有問題,而且也比較簡單;第二種方法在App Store也是沒問題的,但是稍微復雜一些;第三種的話是需要冒風險的,但是如果你的結構太復雜了,導致使用前兩種辦法人為很難控制的話,可以嘗試簡單的使用第三種辦法。
二、屏幕自適應重力感應進行旋轉,實現對每個viewController的單獨控制:
-
子類化UINavigationController,增加方法
- (BOOL)shouldAutorotate { return self.topViewController.shouldAutorotate; } - (NSUInteger)supportedInterfaceOrientations { return self.topViewController.supportedInterfaceOrientations; }
-
并且設定其為程序入口,或指定為 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; }
從而實現了對每個view controller的單獨控制。
-
順便提一下,如果整個應用所有view controller都不支持旋屏,那么干脆:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskPortrait; }