iOS

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

2017-02-10  本文已影响72人  和谐共处

代码如下(需要用到Masonry第三方布局库)

#import "MainVC.h"

@interface MainVC ()

@property (nonatomic,strong) UIView *playView;

@property (nonatomic,strong) UIButton *fullBtn;

@end

@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;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读