iOS-AutoLayout佈局螢幕旋轉(全屏播放)
阿新 • • 發佈:2021-05-28
程式碼如下(需要用到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