轉自:http://blog.csdn.net/wudizhukk/article/details/8674393spa
得到當前屏幕方向.net
self.interfaceOrientation或[[UIApplication sharedApplication] statusBarOrientation]code
if (self.interfaceOrientation==UIDeviceOrientationLandscapeRight) {視頻
XXOOblog
}it
不旋轉,保持豎屏io
//iOS 5-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return(toInterfaceOrientation ==UIInterfaceOrientationPortrait);}//iOS 6-(BOOL)shouldAutorotate { return NO;}-(NSUInteger)supportedInterfaceOrientations { returnUIInterfaceOrientationMaskPortrait;}-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { returnUIInterfaceOrientationPortrait;}
始終保持橫屏ast
//iOS 5-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return(toInterfaceOrientation ==self.preferredInterfaceOrientationForPresentation);}//iOS 6-(BOOL) shouldAutorotate { return YES;}-(NSUInteger)supportedInterfaceOrientations { returnUIInterfaceOrientationMaskLandscapeRight;}-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { returnUIInterfaceOrientationLandscapeRight;}
在使用UINavigationController時發現,不管怎麼設置上面方法的返回,都沒法控制UINavigationController的旋轉,這彷佛是iOS的一個bug。但不管如何,如下是stackflow上面的一個解決方法:
class
1 @implementation UINavigationController (Rotation_IOS6) 2 -(BOOL)shouldAutorotate { 3 return [[self.viewControllers lastObject] shouldAutorotate]; 4 } 5 6 -(NSUInteger)supportedInterfaceOrientations { 7 return [[self.viewControllers lastObject] supportedInterfaceOrientations]; 8 } 9 10 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { 11 return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; 12 } 13 @end
屏幕旋轉方法調用流程bug
要翻轉的時候,首先響應的方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return YES則支持翻轉,NO則不支持。
緊接着
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
被調用。這個方法是發生在翻轉開始以前。通常用來禁用某些控件或者中止某些正在進行的活動,好比中止視頻播放。
再來是
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
這個方法發生在翻轉的過程當中,通常用來定製翻轉後各個控件的位置、大小等。能夠用另外兩個方法來代替:willAnimateFirstHalfOfRotationToInterfaceOrientation:duration: 和 willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
最後調用的是
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation這個方法發生在整個翻轉完成以後。通常用來從新啓用某些控件或者繼續翻轉以前被暫停的活動,好比繼續視頻播放