美文网首页
屏幕旋转设置

屏幕旋转设置

作者: KermitX | 来源:发表于2016-11-01 15:20 被阅读14次

    在做视频的时候需要视频所在的页面可以横屏,但是整个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];
            
        }
    }
    

    相关文章

      网友评论

          本文标题:屏幕旋转设置

          本文链接:https://www.haomeiwen.com/subject/maltuttx.html