iOS强制切换横屏、竖屏

切换横竖屏最直接的方式是调用device的setOrientation方法。但是从sdk3.0以后,这个方法转为似有API,如果要上AppStore的话,要慎用!

 

    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {

        [[UIDevice currentDevice] performSelector:@selector(setOrientation:)

                                       withObject:(id)UIInterfaceOrientationLandscapeRight];

    }

第二种方式是手动的设置界面元素的旋转,包括状态栏、导航栏和视图。以下代码为从竖屏设置为横屏,坐标系是以竖屏的为基准,所以会出现负数的坐标值。

 

    //设置状态栏旋转

    [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:YES];

    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;

    //设置旋转动画

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:duration];    

    //设置导航栏旋转

    self.navigationController.navigationBar.frame = CGRectMake(-204, 224, 480, 32);

    self.navigationController.navigationBar.transform = CGAffineTransformMakeRotation(M_PI*1.5);

    //设置视图旋转

    self.view.bounds = CGRectMake(0, -54, self.view.frame.size.width, self.view.frame.size.height);

    self.view.transform = CGAffineTransformMakeRotation(M_PI*1.5);

    [UIView commitAnimations];

 

来源: <http://blog.csdn.net/dickenslian/article/details/7407221>

 

*********************************************************************************************************************

 

一直遇到这个问题,今天终于找到了解决方法.

在我们的项目中经常遇到横竖屏切换,而又有某个特定的界面必须是特定的显示方式(横屏或竖屏).这就需要如下的处理了.

强制转成横屏:

 

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {

            SEL selector = NSSelectorFromString(@"setOrientation:");

            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];

            [invocation setSelector:selector];

            [invocation setTarget:[UIDevice currentDevice]];

            int val = UIInterfaceOrientationLandscapeRight;

            [invocation setArgument:&val atIndex:2];

            [invocation invoke];

        }

 

以上代码支持ARC哟.

方法二: 通过判断状态栏来设置视图的transform属性。

 

 

- (void)deviceOrientationDidChange: (NSNotification *)notification

{

    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

    CGFloat startRotation = [[self valueForKeyPath:@"layer.transform.rotation.z"] floatValue];

    CGAffineTransform rotation;

    switch (interfaceOrientation) {

        case UIInterfaceOrientationLandscapeLeft:

            rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 270.0 / 180.0);

            break;

        case UIInterfaceOrientationLandscapeRight:

            rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 90.0 / 180.0);

            break;

        case UIInterfaceOrientationPortraitUpsideDown:

            rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 180.0 / 180.0);

            break;

        default:

            rotation = CGAffineTransformMakeRotation(-startRotation + 0.0);

            break;

    }

    view.transform = rotation;

}

posted @ 2016-03-14 10:47  抓狂的ZXY  阅读(2571)  评论(0编辑  收藏  举报