iOS-AutoLayout布局屏幕旋转(全屏播放)

本文介绍了一个使用Swift和Masonry布局库实现的iOS视频播放器。该播放器可根据设备方向变化调整其大小,并通过一个按钮实现全屏和窗口模式之间的切换。
代码如下(需要用到Masonry第三方布局库)
#import "MainVC.h"

[@interface](https://my.oschina.net/u/996807) MainVC ()

[@property](https://my.oschina.net/property) UIView *playView;

[@property](https://my.oschina.net/property) UIButton *fullBtn;

[@end](https://my.oschina.net/u/567204) 


@implementation MainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
  
    _playView = [[UIView alloc]init];
    _playView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_playView];
    [_playView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(200);
    }];
    
    UILabel *title = [[UILabel alloc]init];
    title.text = @"视频标题";
    [_playView addSubview:title];
    [title mas_updateConstraints:^(MASConstraintMaker *make) {
      
        make.centerX.equalTo(_playView.mas_centerX);
        make.centerY.equalTo(_playView.mas_centerY);
        
    }];

    
    UILabel *time = [[UILabel alloc]init];
    time.text = @"08:30/30:00";
    [_playView addSubview:time];
    [time mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_playView).offset(10);
        make.bottom.equalTo(_playView).offset(0);
    }];
    
    _fullBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_fullBtn setTitle:@"大" forState:UIControlStateNormal];
    [_fullBtn setTitle:@"小" forState:UIControlStateSelected];
    [_fullBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [_fullBtn addTarget:self action:@selector(changeDevice) forControlEvents:UIControlEventTouchUpInside];
    [_playView addSubview:_fullBtn];
    [_fullBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(_playView).offset(-10);
        make.bottom.equalTo(_playView).offset(0);
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)changeDevice{
    
    _fullBtn.selected = !_fullBtn.selected;
    if (_fullBtn.selected) {
         [[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeLeft) forKey:@"orientation"];

    }else{
         [[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
    }
    
}
- (void)deviceChange{
    
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;

    switch (deviceOrientation) {
            
        case UIDeviceOrientationPortrait:
        {
            _fullBtn.selected  = NO;
            [_playView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(200);
            }];
        }
            break;
        case UIDeviceOrientationLandscapeLeft:
        {
            _fullBtn.selected  = YES;

            [_playView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(self.view.bounds.size.height);
            }];
        }
            break;
        case UIDeviceOrientationLandscapeRight:
        {
            _fullBtn.selected  = YES;

            [_playView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(self.view.bounds.size.height);
            }];
        }
            break;
            
        default:
            break;
    }
}

转载于:https://my.oschina.net/hehuiqi/blog/1591195

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值