强制横屏方法

2018-04-08  本文已影响210人  崔又淇

强制横屏:

方法一:

关于强制横屏看了很多文章,首先第一个方法是invocation,这个方法可以实现横屏效果,但是后来看了博客都不推荐使用这种方法,而且屏幕旋转的时候,会出现空白的状态

强制横屏方法

这个方法有个问题,现在旋转的时候,页面会出现先竖屏在横屏的状态,不知道哪里出了问题?

源码:

+ (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];

    }

}

方法二:

因为页面的跳转方式全都使用了push方法,所以

- (BOOL)shouldAutorotate   ;

- (UIInterfaceOrientationMask)supportedInterfaceOrientations   ;

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation   ;

不能执行(当然我也不确定是不是我操作的问题)

项目的需求:是在视频页面横屏操作,然后从视频页面push到其他页面,其他页面在push到签字页面,签字页面也需要横屏操作

在网上找到的方法:

第一步:

在appdelegate中声明一个属性@property (nonatomic, assign) BOOL allowRotation;

然后在appdelegate中实现方法

Appdelegate.m

源码:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {

    if (self.allowRotation == YES) {

        return UIInterfaceOrientationMaskLandscapeRight;

    }else{

        return UIInterfaceOrientationMaskPortrait;

    }

}

第二步:

在viewcontroller页面中实现方法,实现旋转屏幕

源码:

- (void)setNewOrientation:(BOOL)fullscreen{

    if (fullscreen) {

        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];

        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];

        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

    }else{

        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];

        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];

        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

    }

}

第三步:

在需要旋转屏幕页面初始化的时候,调用横屏方法

       AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

        appDelegate.allowRotation = YES;//(以上2行代码,可以理解为打开横屏开关)

        [self setNewOrientation:YES];//调用转屏代码

在页面返回按钮的操作中,将屏幕返回竖屏状态

       AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

        appDelegate.allowRotation = NO;//(以上2行代码,可以理解为打开横屏开关)

        [self setNewOrientation:NO];//调用转屏代码

方法三:

使用view的动画效果实现屏幕旋转,但是整个系统还依然是竖屏状态, 最后将此方法修改了.这里记录下view的动画效果

view动画效果

源码:

   self.originFrame = self.view.frame;

    CGFloat height = [[UIScreen mainScreen] bounds].size.width;

    CGFloat width = [[UIScreen mainScreen] bounds].size.height;

    CGRect frame = CGRectMake((height - width) / 2, (width - height) / 2, width, height);

    self.frame = frame;

    [UIView animateWithDuration:0.3f animations:^{

        self.frame = frame;

        [self.view setTransform:CGAffineTransformMakeRotation(M_PI_2)];

    } completion:^(BOOL finished) {

    }];

上一篇下一篇

猜你喜欢

热点阅读