IOS强制屏幕横竖屏相互切换

最近项目要做一个html5电子协议,里面涉及到签名,竖屏签名不够,所以需要把屏幕切换到横屏,签完字后把签字内容返回到竖屏中的方框内,由于项目不上AppStore,只用企业证书打包,所以使用下面方式来实现横竖屏切换功能。

bool isPortrait = true;
- (IBAction)changeOri:(id)sender {
    if (isPortrait) {
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
            [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeLeft]];
            isPortrait = false;
        }
    }else{
        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 = UIDeviceOrientationPortrait;
            //从2开始是因为0 1 两个参数已经被selector和target占用
            [invocation setArgument:&val atIndex:2];
            [invocation invoke];
            isPortrait = true;
        }
    }
}


上面那种情况如果没有选中竖屏、横屏左或横屏右的话就会失效,下面方法不会,是通用的方法

- (BOOL)shouldAutorotate{
    return NO;
}

bool isPortrait = true;
- (IBAction)changeOri:(id)sender {
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    [UIView animateWithDuration:duration animations:^{
        [[UIApplication sharedApplication] setStatusBarOrientation:isPortrait ? UIInterfaceOrientationLandscapeRight : UIInterfaceOrientationPortrait];
        self.navigationController.view.transform = isPortrait ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformIdentity;
        self.navigationController.view.bounds = CGRectMake(self.navigationController.view.bounds.origin.x, self.navigationController.view.bounds.origin.y, self.view.frame.size.height, self.view.frame.size.width);
        isPortrait = !isPortrait;   
    }];
}



你可能感兴趣的:(IOS强制屏幕横竖屏相互切换)