第一種:經過人爲的辦法改變view.transform的屬性。app
具體辦法:ide
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的屬性,而每次改變以前沒有重置的話,你會發現後來 的改變和你想要的發生變化了,不是你真正想要的結果。spa
好了,上面介紹了旋轉的屬性,接下來就是關鍵了。官方提供了一個辦法就是查看當前電池條的狀態UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;經過這個辦法,你能夠知道當前屏幕的電池條的顯示方向,並且你還能夠 強制設置他的顯示方向,經過設置這個屬性就OK了,能夠選擇是否動畫改變電池條方向。有了這兩個那咱們就能夠任意的改變咱們想要的顯示方式了。orm
1.獲取當前電池條的方向UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation文檔
2.獲取當前屏幕的大小CGRect frame = [UIScreen mainScreen].applicationFrame;it
3.設置咱們的View的中心點
CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2));io
4.根據當前電池條的方向,獲取須要旋轉的角度的大小。一般form
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;
}stream
5.能夠動畫的改變咱們view的顯示方式了
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:YES];
CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;(獲取當前電池條動畫改變的時間)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
//在這裏設置view.transform須要匹配的旋轉角度的大小就能夠了。
[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確定沒問問題,可是第二種的話是須要冒風險的,可是若是你的結構太複雜了,致使使用第一種辦法人爲很難控制的話,能夠嘗試簡單的使用第二種辦 法。我在有米提供的sample裏面就看到他使用了第二種簡單的辦法來顯示橫屏豎式的廣告條。