美文网首页
强制横屏方法

强制横屏方法

作者: 崔又淇 | 来源:发表于2018-04-08 17:27 被阅读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) {

    }];

相关文章

  • 强制横屏方法

    强制横屏: 方法一: 关于强制横屏看了很多文章,首先第一个方法是invocation,这个方法可以实现横屏效果,但...

  • 横竖屏

    需求: 让push的ViewController界面强制横屏 一、配置 二、添加强制横屏方法 pragma mar...

  • iOS 部分界面强制横屏与强制竖屏

    最新屏幕强制旋转详见 强制横屏(此方法为旋转视图) 恢复竖屏

  • iOS 强制横竖屏

    强制横屏 强制竖屏 ⚠️ 在执行上述方法时要重写controller的一下方法

  • iOS 强制横屏和竖屏

    感谢这位同学的文章 需求:只有一个页面横屏而且是强制横屏,其他页面竖屏而且强制竖屏。 说明:本文方法只适用于pre...

  • iOS 视频横竖屏窗口解决方案

    1.其他界面是竖屏,有个界面只支持横屏 #pragma mark 强制横屏的方法 - (BOOL)shouldAu...

  • iOS 强制横屏(Push和模态)

    # iOS 强制横屏(Push和模态) iOS开发过程中,有时候需要页面强制横屏。 下面这种方法是不管手机有没有开...

  • iOS横屏的深入研究

    iOS横屏模式分2种:跟随系统自动旋转、强制横屏。无论哪种横屏模式,都有2中实现途径:1.重写系统旋转方法。2.对...

  • iOS:强制横屏的坑

    前段时间我们播放器强制横屏,项目设置允许竖屏,在手机不锁屏状态下,手机横屏会导致播放器强制横屏的时候会导致横屏失败...

  • iOS强制横屏

    iOS强制横屏

网友评论

      本文标题:强制横屏方法

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