iOS 强制横屏、部分横屏等功能实践

需求如下:

1.app整体只能竖屏,部分页面才可以横屏
2.app整体只能竖屏,部分页面也是竖屏,但是点击某个按钮可以使当前页面变为横屏,如全屏视频播放键。

需求1解决方法:

1.在targets - general中设置设备支持方向如下,确保设备各个方法均支持

屏幕快照 2016-12-08 下午6.41.26.png

2.在appdelegate的.h文件声明属性来标记当前设备方向

@property (assign , nonatomic)UIInterfaceOrientation interfaceOrientation;

在appdelegate的.m文件中:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
    
    if(self.interfaceOrientation == UIInterfaceOrientationUnknown) {
 // 直播间用
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else if(self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
//交易页面横屏用
        return UIInterfaceOrientationMaskLandscapeRight;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

3.在需要横屏的ViewController的ViewWillApper方法里面:

 

 Appdelegate *delegate =p.p1[UIApplication sharedApplication].delegate;
delegate.canRotate = YES;

需求2解决方法:

方法1:
1.在targets - general中设置设备支持方向如下,确保整体都是竖屏

屏幕快照 2016-12-08 下午6.30.25.png

2.在横屏(如button点击)事件处,代码如下



- (void)fullScreenBtnAction {
    if (_isNormalOrientation) {   // 如果当前是默认的竖屏
        //注:当前self是UIView对象,如果是VC,则为self.view.....
        self.frame = CGRectMake(0, 0, kScreenSize.height, kScreenSize.width);
        CGRect frame = [UIScreen mainScreen].applicationFrame;
        // transfrom会以当前center为锚点旋转,所以旋转后位置有偏移,需要处理
        CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2));
        self.center = center;       
        //取状态栏旋转时间
//        CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration: 0.3];
        self.transform = CGAffineTransformMakeRotation(M_PI_2);
        [UIView commitAnimations];
    } else {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration: 0.3];
        self.transform = CGAffineTransformIdentity;
        [UIView commitAnimations];
        //_originRect是进入横屏前self的frame
        self.frame = _originRect;
    }
    _isNormalOrientation = !_isNormalOrientation; //更改状态
}

***方法2:
1.在targets - general中设置设备支持方向如下,确保设备各个方法均支持 (弃用 可不设置)


屏幕快照 2016-12-08 下午6.41.26.png

2.在appdelegate的.h文件声明属性interfaceOrientation标记方向

@property (assign , nonatomic)UIInterfaceOrientation interfaceOrientation;

在appdelegate的.m文件中实现设备方向方法:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
    
    if(self.interfaceOrientation == UIInterfaceOrientationUnknown) { // 直播间用
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else if(self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) { //交易页面横屏用
        return UIInterfaceOrientationMaskLandscapeRight;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}
  1. 横竖屏转换:
//转换到竖屏
- (void)rotateToPortraitScreenWithInvocation {
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    SEL selector = NSSelectorFromString(@"setOrientation:");
    
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
    
    [invocation setSelector:selector];
    
    [invocation setTarget:[UIDevice currentDevice]];
    
    int val = UIDeviceOrientationPortrait;
    
    [invocation setArgument:&val atIndex:2];
    
    [invocation invoke];
    
    delegate.interfaceOrientation = UIInterfaceOrientationPortrait;
}

//转换到横屏模式
- (void)rotateToLandscapWithIncovation {
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    delegate.interfaceOrientation = UIInterfaceOrientationUnknown;
    
    SEL selector = NSSelectorFromString(@"setOrientation:");
    
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
    
    [invocation setSelector:selector];
    
    [invocation setTarget:[UIDevice currentDevice]];
    
    int val = UIInterfaceOrientationLandscapeRight;
    
    [invocation setArgument:&val atIndex:2];
    
    [invocation invoke];
}




补充后来自己又折腾的几个Demo:
https://github.com/3KK3/AppdelegateMethodHorizDemo/tree/master

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • iOS 知识小集(横竖屏切换) 转载自 http://www.cocoachina.com/ios/2016072...
    Poison_19ce阅读 3,873评论 0 0
  • 1.监听屏幕旋转方向 在处理iOS横竖屏时,经常会和UIDeviceOrientation、UIInterface...
    彬至睢阳阅读 7,296评论 1 6
  • 当iphone的某一方向(一般是竖屏)锁定没有开启的时候,当手机横放后,没有写死程序支持方向的程序或者支持横屏的程...
    SuperZico阅读 12,820评论 0 4
  • 概述 写代码就是在不断填坑的过程中慢慢成长,程序员哪有不遇坑的呢? 这篇文章来谈谈iOS中横竖屏切换的一些坑,横竖...
    jumpingfrog0阅读 13,889评论 6 21
  • iOS 中横竖屏切换的功能,在开发iOS app中总能遇到。以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰...
    零度_不结冰阅读 6,648评论 0 0

友情链接更多精彩内容