// // SPSBSelectSinglePhotoViewController.m // 我的社保 // // Created by shanp on 2021/5/13. // #import "SPSBSelectSinglePhotoViewController.h" #import "SPSBUIGeneralHeader.h" #import "UIViewController+SPSBInitialization.h" #import #define WIDTH jxh_screenWidth() #define HEIGHT (jxh_screenHeight() - jxh_safeInsets(spsb_keyWindow()).bottom) NSNotificationName const SPSBReopenCameraNotification = @"SPSBReopenCameraNotification"; @interface SPSBSelectSinglePhotoViewController () { void(^_passValue)(NSValue *value); UIScrollView *_backgroundScrollView; CGFloat _showHeight; UIImageView *_imageView; __weak UINavigationController *_self_nav; } @end @implementation SPSBSelectSinglePhotoViewController - (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //取消滑动手势返回 _self_nav = self.navigationController; self.navigationController.interactivePopGestureRecognizer.enabled = false; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; //启动滑动手势返回 _self_nav.interactivePopGestureRecognizer.enabled = true; } - (BOOL)prefersStatusBarHidden { return true; } #pragma mark - Action - (void)buttonAction:(UIButton *)sender { if (sender.tag == 2002) { CGRect rect; if (_spsb_proportion >= 0.5) { CGFloat f = jxh_viewWidth(_imageView) / _spsb_dataImage.size.width; if (_spsb_dataImage.size.height >= _spsb_dataImage.size.width) { rect = CGRectMake(_backgroundScrollView.contentOffset.x / f, _backgroundScrollView.contentOffset.y / f + _backgroundScrollView.contentInset.top / f, WIDTH / f, WIDTH / _spsb_proportion / f); } else { CGFloat h = _imageView.frame.size.width / _spsb_dataImage.size.width * _spsb_dataImage.size.height; if (h <= WIDTH / _spsb_proportion) { rect = CGRectMake(_backgroundScrollView.contentOffset.x / f, 0, WIDTH / f, h / f); } else { rect = CGRectMake(_backgroundScrollView.contentOffset.x / f, _backgroundScrollView.contentOffset.y / f + _backgroundScrollView.contentInset.top / f, WIDTH / f, WIDTH / _spsb_proportion / f); } } } else { rect = CGRectMake(0, 0, _spsb_dataImage.size.width, _spsb_dataImage.size.height); } //加快界面过度,防止图片的处理工作导致界面停顿,先发数据,界面消息后再处理数据 _passValue([NSValue valueWithCGRect:rect]); if (_spsb_needToDismiss) { [self.navigationController.viewControllers[0] dismissViewControllerAnimated:true completion:^{ self->_passValue(nil); }]; } } else { UIImagePickerController *imagePicker = (UIImagePickerController *)self.navigationController; if (imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) { [self.navigationController.viewControllers[0] dismissViewControllerAnimated:false completion:^{ jxh_postNotification(SPSBReopenCameraNotification, nil, nil); }]; } else { [self.navigationController popViewControllerAnimated:true]; } } } - (void)passValue:(void(^)(NSValue *value))value { _passValue = value; } #pragma mark - Network Action #pragma mark - UIScrollViewDelegate - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return _imageView; } - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale { CGFloat spsb_proportion = _spsb_proportion; if (spsb_proportion <= 0.5) { spsb_proportion = WIDTH / HEIGHT; } if (_spsb_dataImage.size.height >= _spsb_dataImage.size.width) { scrollView.contentInset = UIEdgeInsetsMake(- (HEIGHT - WIDTH / spsb_proportion) / 2 * (scale - 1), 0, - (HEIGHT - WIDTH / spsb_proportion) / 2 * (scale - 1), 0); } else { CGFloat h = _imageView.frame.size.width / _spsb_dataImage.size.width * _spsb_dataImage.size.height; if (h <= WIDTH / spsb_proportion) { scrollView.contentInset = UIEdgeInsetsMake(- (jxh_viewHeight(_imageView) - HEIGHT) / 2, 0, - (jxh_viewHeight(_imageView) - HEIGHT) / 2, 0); } else { CGFloat h = WIDTH / _spsb_dataImage.size.width * _spsb_dataImage.size.height; CGFloat c = (WIDTH / spsb_proportion - h) / 2 * scale; scrollView.contentInset = UIEdgeInsetsMake(- (HEIGHT - WIDTH / spsb_proportion) / 2 * (scale - 1) - c, 0, - (HEIGHT - WIDTH / spsb_proportion) / 2 * (scale - 1) - c, 0); } } } - (void)scrollViewDidZoom:(UIScrollView *)scrollView { CGSize boundsSize = scrollView.bounds.size, contentSize = scrollView.contentSize; CGFloat offsetX = (boundsSize.width > contentSize.width) ? (boundsSize.width - contentSize.width) / 2 : 0.0; _imageView.center = CGPointMake(contentSize.width / 2 + offsetX, _imageView.center.y); } #pragma mark - UI - (void)setupUI { self.spsb_isHiddenNavigationBar = true; self.view.backgroundColor = spsb_000000_color(1.f); self.automaticallyAdjustsScrollViewInsets = true; [self addImageView]; [self addSelectMenuView]; if (_spsb_proportion > 0.1) { [self addSelectAreaView]; } } - (void)addImageView { _backgroundScrollView = [[UIScrollView alloc] init]; _backgroundScrollView.delegate = self; _backgroundScrollView.showsHorizontalScrollIndicator = false; _backgroundScrollView.showsVerticalScrollIndicator = false; _backgroundScrollView.maximumZoomScale = 2.0; _backgroundScrollView.minimumZoomScale = 1.0; if (@available(iOS 11.0, *)) { _backgroundScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } [self.view addSubview:_backgroundScrollView]; [_backgroundScrollView makeConstraints:^(JXHConstraintMaker *make) { make.top.and.leading.and.trailing.equalTo(0); make.bottom.equalTo(self.view.safebottom); }]; _imageView = [[UIImageView alloc] initWithImage:_spsb_dataImage]; _imageView.contentMode = UIViewContentModeScaleAspectFit; [_backgroundScrollView addSubview:_imageView]; _showHeight = WIDTH / _spsb_dataImage.size.width * _spsb_dataImage.size.height; if (_spsb_proportion > 0.5) { if (_spsb_dataImage.size.height >= _spsb_dataImage.size.width) { [_imageView makeConstraints:^(JXHConstraintMaker *make) { make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0)); make.size.equalTo(CGSizeMake(WIDTH, self->_showHeight + HEIGHT - WIDTH / self->_spsb_proportion)); }]; } else { CGFloat c = (WIDTH / _spsb_proportion - _showHeight) * _backgroundScrollView.zoomScale; [_imageView makeConstraints:^(JXHConstraintMaker *make) { make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0)); make.size.equalTo(CGSizeMake(WIDTH, self->_showHeight + c + HEIGHT - WIDTH / self->_spsb_proportion)); }]; } } else { [_imageView makeConstraints:^(JXHConstraintMaker *make) { make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0)); make.size.equalTo(CGSizeMake(WIDTH, HEIGHT)); }]; } } - (void)addSelectAreaView { UIView *view = [[UIView alloc] init]; view.userInteractionEnabled = false; [self.view addSubview:view]; [view makeConstraints:^(JXHConstraintMaker *make) { make.size.equalTo(CGSizeMake(WIDTH - jxh_onePixe() * 2, WIDTH / self->_spsb_proportion - jxh_onePixe() * 2)); make.center.equalTo(CGPointMake(0, -jxh_safeInsets(spsb_keyWindow()).bottom / 2)); }]; [view setLayerBorderWidth:jxh_onePixe() * 2 borderColor:spsb_FFFFFF_color(1.f)]; } - (void)addSelectMenuView { UIView *menuView = [[UIView alloc] init]; menuView.backgroundColor = spsb_666666_5_color(); [self.view addSubview:menuView]; [menuView makeConstraints:^(JXHConstraintMaker *make) { make.leading.and.trailing.equalTo(0); make.bottom.equalTo(self.view.safebottom); make.height.equalTo(60); }]; NSArray *titleArray = @[@"取消", @"选取"]; for (NSInteger i = 0; i < 2; i ++) { UIButton *button = [UIButton convenienceWithFont:spsb_font(15) target:self action:@selector(buttonAction:)]; [button setTitle:titleArray[i] titleColor:spsb_FFFFFF_color(1.f) state:JXHButtonControlStateNormal]; button.tag = 2001 + i; [menuView addSubview:button]; [button makeConstraints:^(JXHConstraintMaker *make) { if (i == 0) { make.leading.equalTo(0); } else { make.trailing.equalTo(0); } make.top.and.bottom.equalTo(0); make.width.equalTo(60); }]; } } @end