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;   
    }];
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值