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

2017-04-19  本文已影响0人  iOS_Edward

在网上找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对你有所帮助.

上一篇下一篇

猜你喜欢

热点阅读