IOS強制屏幕橫豎屏相互切換 – iPhone手機開發 iPhone軟體開發教學課程

最近項目要做一個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;
        }
    }
}

 

發佈留言