// // YMCustomCameraViewController.m // MSYOUPAI // // Created by YoMi on 2024/3/17. // Copyright © 2024 MS. All rights reserved. // #import "YMCustomCameraViewController.h" #import "YMCustomCameraViewModel.h" #import "YMCustomCameraTopFunctionView.h" #import "YMCustomCameraBottomFunctionView.h" #import "YMAuthenticationCenterViewController.h" @interface YMCustomCameraViewController () /// 自定义相机VM @property (nonatomic, strong) YMCustomCameraViewModel *viewModel; /// 摄像机底部功能视图 @property (nonatomic, strong) YMCustomCameraTopFunctionView *cameraTopFunctionView; /// 摄像机捕获区域遮罩视图 @property (nonatomic, strong) UIView *cameraCaptureAreaView; /// 摄像机示例照视图 @property (nonatomic, strong) UIImageView *cameraSamplePhotoView; /// 摄像机底部功能视图 @property (nonatomic, strong) YMCustomCameraBottomFunctionView *cameraBottomFunctionView; @end @implementation YMCustomCameraViewController @dynamic viewModel; - (void)viewDidLoad { [super viewDidLoad]; [self setNavHidden:YES]; self.view.backgroundColor = HexColorFromRGB(0x000000); } - (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [self deletUnwantedVC]; } /** 在页面结束或出现记得开启/停止摄像*/ - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; if (![self.viewModel.cameraManager.session isRunning]) { [self.viewModel.cameraManager.session startRunning]; } } - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; if ([self.viewModel.cameraManager.session isRunning]) { [self.viewModel.cameraManager.session stopRunning]; } } - (void)ym_setupViews{ [self.view addSubview:self.cameraTopFunctionView]; [self.view addSubview:self.cameraCaptureAreaView]; [self.view addSubview:self.cameraSamplePhotoView]; [self.view addSubview:self.cameraBottomFunctionView]; [self.view setNeedsUpdateConstraints]; [self.view updateConstraintsIfNeeded]; } - (void)updateViewConstraints{ [self.cameraTopFunctionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(kYMStatusBarHeight); make.left.equalTo(self.view); make.right.equalTo(self.view); make.height.mas_equalTo(adapt(45)); }]; [self.cameraCaptureAreaView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.cameraTopFunctionView.mas_bottom); make.left.equalTo(self.view); make.right.equalTo(self.view); make.bottom.equalTo(self.cameraBottomFunctionView.mas_top); }]; [self.cameraSamplePhotoView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view); make.bottom.equalTo(self.cameraBottomFunctionView.mas_top); make.width.height.mas_equalTo(adapt(120)); }]; [self.cameraBottomFunctionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view); make.right.equalTo(self.view); make.bottom.equalTo(self.view); make.height.mas_equalTo(adapt(100)); }]; [super updateViewConstraints]; } - (void)ym_bindViewModel{ [self.cameraTopFunctionView ym_bindViewModel:self.viewModel]; [self.cameraBottomFunctionView ym_bindViewModel:self.viewModel]; [self.viewModel getSamplePhotoData]; [self.viewModel.cameraManager configureWithParentLayer:self.cameraCaptureAreaView]; @weakify(self) [[[[RACObserve(self.viewModel, samplePhotoUrl) distinctUntilChanged] deliverOnMainThread] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSString * samplePhotoUrl) { @strongify(self) [self.cameraSamplePhotoView sd_setImageWithURL:[LCTools getImageUrlWithAddress:samplePhotoUrl] placeholderImage:[UIImage new] options:SDWebImageRefreshCached]; }]; } #pragma mark - 删除不需要的视图控制器 - (void)deletUnwantedVC{ for (UIViewController *vc in self.navigationController.viewControllers) { if ([vc isKindOfClass:[YMAuthenticationCenterViewController class]]) { [vc removeFromParentViewController]; } } } /// 点击对焦 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self.view]; [self.viewModel.cameraManager focusInPoint:point]; } - (YMCustomCameraTopFunctionView *)cameraTopFunctionView{ if (!_cameraTopFunctionView) { _cameraTopFunctionView = [[YMCustomCameraTopFunctionView alloc]init]; } return _cameraTopFunctionView; } - (UIView *)cameraCaptureAreaView{ if (!_cameraCaptureAreaView) { _cameraCaptureAreaView = [[UIView alloc]initWithFrame:CGRectMake(0, kYMStatusBarHeight, kFrameWidth, kFrameHeight - kYMStatusBarHeight - adapt(45) - adapt(100))]; } return _cameraCaptureAreaView; } - (UIImageView *)cameraSamplePhotoView{ if (!_cameraSamplePhotoView) { _cameraSamplePhotoView = [[UIImageView alloc]init]; } return _cameraSamplePhotoView; } - (YMCustomCameraBottomFunctionView *)cameraBottomFunctionView{ if (!_cameraBottomFunctionView) { _cameraBottomFunctionView = [[YMCustomCameraBottomFunctionView alloc]init]; } return _cameraBottomFunctionView; } @end