屏幕旋转设置

在做视频的时候需要视频所在的页面可以横屏,但是整个APP中其他页面都是不允许横屏的,这个时候要怎么做呢?

AppDelegate.h
@property (nonatomic, assign) BOOL allowRotation;
AppDelegate.m
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  {

    if (self.allowRotation) {
        return UIInterfaceOrientationMaskPortrait|
        UIInterfaceOrientationMaskLandscapeLeft|
        UIInterfaceOrientationMaskLandscapeRight;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}
视频详情页:
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    SET_UIStatusBarStyleLightContent;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    // 允许横屏操作
    [(AppDelegate*)[UIApplication sharedApplication].delegate setAllowRotation:YES];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (_needInputTime) {
        [self inputPlayTime];
    }
    
    // 禁止横屏操作
    [(AppDelegate*)[UIApplication sharedApplication].delegate setAllowRotation:NO];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    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 = UIInterfaceOrientationPortrait;
        
        [invocation setArgument:&val atIndex:2];
        
        [invocation invoke];
        
    }
}

你可能感兴趣的:(屏幕旋转设置)