ios 手动点击按钮实现横竖屏幕的旋转

762 阅读1分钟
//按钮点击横屏
-(void)leftAction{
    [self interfaceOrientation:UIInterfaceOrientationLandscapeLeft];
}

//按钮点击竖屏
-(void)rightAction{
    [self interfaceOrientation:UIInterfaceOrientationPortrait];
}




-(BOOL)shouldAutorotate{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft);
}

- (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
    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 = orientation;
        // 从2开始是因为0 1 两个参数已经被selector和target占用
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}


实现效果图