IOS疯狂基础之屏幕旋转控制,得到当前方向(转)

转自: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这个方法发生在整个翻转完成以后。通常用来从新启用某些控件或者继续翻转以前被暂停的活动,好比继续视频播放

相关文章
相关标签/搜索