iOS横竖屏切换的问题解决(视屏播放界面效果)

在网上找iOS横屏相关的信息时发现只有进入页面强制横屏的代码,并没有在一个页面通过点击按钮的方式实现当前屏幕的横竖屏的代码,其实原来在上家公司时就有做过一个横屏的游戏app,当时就对屏幕强制横屏做过一些研究,现在把它整理处理希望对读者处理横竖屏相关有所帮助.

PS:首先在app在创建时应注意对该app横竖屏的约束没有问题,否则在进行横竖屏时不会有效果

Snip20170419_1.png

1.如果我们的需求进入该页面就直接横屏并且一直横屏时,可以在该页面添加如下代码

- (BOOL)shouldAutorotate {
 return YES;
}

- (UIInterfaceOrientationMask) supportedInterfaceOrientations {
 return UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
 return UIInterfaceOrientationLandscapeRight;
}

2.如果需求是类似视屏播放界面可以通过点击按钮来实现当前页面的横竖屏切换,demo如下

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end
#import "ViewController.h"

@interface ViewController (){

    BOOL _isCanAutoOrientation;
    UIButton * _btn;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   [UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor whiteColor];
    
    //创建一个按钮
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];
    _btn = btn;
    [btn setBackgroundColor:[UIColor blueColor]];
    [btn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

-(void)btnclick {

    _isCanAutoOrientation = !_isCanAutoOrientation;
    
    //屏幕横屏的方法
    if (_isCanAutoOrientation) {
        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];
        NSLog(@"横屏");
//        _btn.frame = CGRectMake(0, 0, 100, 50);
       
    //屏幕竖屏的方法
    }else {
    
        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];
        NSLog(@"竖屏");
//        _btn.frame = CGRectMake(100, 100, 200, 50);
    }
}


#pragma mark - 屏幕Autorotate
-(BOOL)shouldAutorotate{
    return YES;
}

#pragma mark 屏幕支持的旋转方向
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface {
    return (interface == UIInterfaceOrientationPortrait || interface == UIInterfaceOrientationLandscapeRight);
}

#ifdef IOS6

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight;
}
#endif

#pragma mark 支持哪些方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    if (_isCanAutoOrientation) {
        return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeRight;
    }
    
    return UIInterfaceOrientationMaskPortrait;
}

#pragma mark 一开始希望的屏幕方向
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}


#pragma mark - 屏幕即将变换方向
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if([UIDevice currentDevice].systemVersion.intValue >=9.0){
        
        NSLog(@"1");
    
    }
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
    {
        _btn.frame = CGRectMake(100, 100, 200, 50);
    }
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {

        NSLog(@"3");
    }
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        _btn.frame = CGRectMake(0, 0, 400, 100);
    }
}

@end

读者可以直接使用上面的demo体验横竖屏的切换.

希望上面的demo对你有所帮助.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,276评论 4 61
  • iOS 中横竖屏切换的功能,在开发iOS app中总能遇到。以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰...
    零度_不结冰阅读 2,251评论 0 0
  • 作者:徐行 轻轻地 漫步在清凉的月色之下 慢回首 那一排排青瓦 起伏着绵延的白雪 宁静如这般的夜 清澈如月般的心 ...
    文蒻阅读 234评论 1 2
  • 西藏因其高海拔,长日照,低氧度的特殊地貌,以及大片未曾开发的原始环境,每年吸引着大量游客来此游览。 一辈子一定要来...
    黛拉小确幸阅读 473评论 0 10
  • 1.Warning: Attempt to present on whose view is not in t...
    tinghua阅读 344评论 0 0