SPSBSelectSinglePhotoViewController.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // SPSBSelectSinglePhotoViewController.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/13.
  6. //
  7. #import "SPSBSelectSinglePhotoViewController.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #import "UIViewController+SPSBInitialization.h"
  10. #import <NSObject+JXHNotification.h>
  11. #define WIDTH jxh_screenWidth()
  12. #define HEIGHT (jxh_screenHeight() - jxh_safeInsets(spsb_keyWindow()).bottom)
  13. NSNotificationName const SPSBReopenCameraNotification = @"SPSBReopenCameraNotification";
  14. @interface SPSBSelectSinglePhotoViewController ()<UIScrollViewDelegate> {
  15. void(^_passValue)(NSValue *value);
  16. UIScrollView *_backgroundScrollView;
  17. CGFloat _showHeight;
  18. UIImageView *_imageView;
  19. __weak UINavigationController *_self_nav;
  20. }
  21. @end
  22. @implementation SPSBSelectSinglePhotoViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. [self setupUI];
  26. }
  27. - (void)viewDidAppear:(BOOL)animated {
  28. [super viewDidAppear:animated];
  29. //取消滑动手势返回
  30. _self_nav = self.navigationController;
  31. self.navigationController.interactivePopGestureRecognizer.enabled = false;
  32. }
  33. - (void)viewDidDisappear:(BOOL)animated {
  34. [super viewDidDisappear:animated];
  35. //启动滑动手势返回
  36. _self_nav.interactivePopGestureRecognizer.enabled = true;
  37. }
  38. - (BOOL)prefersStatusBarHidden {
  39. return true;
  40. }
  41. #pragma mark - Action
  42. - (void)buttonAction:(UIButton *)sender {
  43. if (sender.tag == 2002) {
  44. CGRect rect;
  45. if (_spsb_proportion >= 0.5) {
  46. CGFloat f = jxh_viewWidth(_imageView) / _spsb_dataImage.size.width;
  47. if (_spsb_dataImage.size.height >= _spsb_dataImage.size.width) {
  48. rect = CGRectMake(_backgroundScrollView.contentOffset.x / f, _backgroundScrollView.contentOffset.y / f + _backgroundScrollView.contentInset.top / f, WIDTH / f, WIDTH / _spsb_proportion / f);
  49. } else {
  50. CGFloat h = _imageView.frame.size.width / _spsb_dataImage.size.width * _spsb_dataImage.size.height;
  51. if (h <= WIDTH / _spsb_proportion) {
  52. rect = CGRectMake(_backgroundScrollView.contentOffset.x / f, 0, WIDTH / f, h / f);
  53. } else {
  54. rect = CGRectMake(_backgroundScrollView.contentOffset.x / f, _backgroundScrollView.contentOffset.y / f + _backgroundScrollView.contentInset.top / f, WIDTH / f, WIDTH / _spsb_proportion / f);
  55. }
  56. }
  57. } else {
  58. rect = CGRectMake(0, 0, _spsb_dataImage.size.width, _spsb_dataImage.size.height);
  59. }
  60. //加快界面过度,防止图片的处理工作导致界面停顿,先发数据,界面消息后再处理数据
  61. _passValue([NSValue valueWithCGRect:rect]);
  62. if (_spsb_needToDismiss) {
  63. [self.navigationController.viewControllers[0] dismissViewControllerAnimated:true completion:^{
  64. self->_passValue(nil);
  65. }];
  66. }
  67. } else {
  68. UIImagePickerController *imagePicker = (UIImagePickerController *)self.navigationController;
  69. if (imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
  70. [self.navigationController.viewControllers[0] dismissViewControllerAnimated:false completion:^{
  71. jxh_postNotification(SPSBReopenCameraNotification, nil, nil);
  72. }];
  73. } else {
  74. [self.navigationController popViewControllerAnimated:true];
  75. }
  76. }
  77. }
  78. - (void)passValue:(void(^)(NSValue *value))value {
  79. _passValue = value;
  80. }
  81. #pragma mark - Network Action
  82. #pragma mark - UIScrollViewDelegate
  83. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  84. return _imageView;
  85. }
  86. - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
  87. CGFloat spsb_proportion = _spsb_proportion;
  88. if (spsb_proportion <= 0.5) {
  89. spsb_proportion = WIDTH / HEIGHT;
  90. }
  91. if (_spsb_dataImage.size.height >= _spsb_dataImage.size.width) {
  92. scrollView.contentInset = UIEdgeInsetsMake(- (HEIGHT - WIDTH / spsb_proportion) / 2 * (scale - 1), 0, - (HEIGHT - WIDTH / spsb_proportion) / 2 * (scale - 1), 0);
  93. } else {
  94. CGFloat h = _imageView.frame.size.width / _spsb_dataImage.size.width * _spsb_dataImage.size.height;
  95. if (h <= WIDTH / spsb_proportion) {
  96. scrollView.contentInset = UIEdgeInsetsMake(- (jxh_viewHeight(_imageView) - HEIGHT) / 2, 0, - (jxh_viewHeight(_imageView) - HEIGHT) / 2, 0);
  97. } else {
  98. CGFloat h = WIDTH / _spsb_dataImage.size.width * _spsb_dataImage.size.height;
  99. CGFloat c = (WIDTH / spsb_proportion - h) / 2 * scale;
  100. scrollView.contentInset = UIEdgeInsetsMake(- (HEIGHT - WIDTH / spsb_proportion) / 2 * (scale - 1) - c, 0, - (HEIGHT - WIDTH / spsb_proportion) / 2 * (scale - 1) - c, 0);
  101. }
  102. }
  103. }
  104. - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  105. CGSize boundsSize = scrollView.bounds.size, contentSize = scrollView.contentSize;
  106. CGFloat offsetX = (boundsSize.width > contentSize.width) ? (boundsSize.width - contentSize.width) / 2 : 0.0;
  107. _imageView.center = CGPointMake(contentSize.width / 2 + offsetX, _imageView.center.y);
  108. }
  109. #pragma mark - UI
  110. - (void)setupUI {
  111. self.spsb_isHiddenNavigationBar = true;
  112. self.view.backgroundColor = spsb_000000_color(1.f);
  113. self.automaticallyAdjustsScrollViewInsets = true;
  114. [self addImageView];
  115. [self addSelectMenuView];
  116. if (_spsb_proportion > 0.1) {
  117. [self addSelectAreaView];
  118. }
  119. }
  120. - (void)addImageView {
  121. _backgroundScrollView = [[UIScrollView alloc] init];
  122. _backgroundScrollView.delegate = self;
  123. _backgroundScrollView.showsHorizontalScrollIndicator = false;
  124. _backgroundScrollView.showsVerticalScrollIndicator = false;
  125. _backgroundScrollView.maximumZoomScale = 2.0;
  126. _backgroundScrollView.minimumZoomScale = 1.0;
  127. if (@available(iOS 11.0, *)) {
  128. _backgroundScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  129. }
  130. [self.view addSubview:_backgroundScrollView];
  131. [_backgroundScrollView makeConstraints:^(JXHConstraintMaker *make) {
  132. make.top.and.leading.and.trailing.equalTo(0);
  133. make.bottom.equalTo(self.view.safebottom);
  134. }];
  135. _imageView = [[UIImageView alloc] initWithImage:_spsb_dataImage];
  136. _imageView.contentMode = UIViewContentModeScaleAspectFit;
  137. [_backgroundScrollView addSubview:_imageView];
  138. _showHeight = WIDTH / _spsb_dataImage.size.width * _spsb_dataImage.size.height;
  139. if (_spsb_proportion > 0.5) {
  140. if (_spsb_dataImage.size.height >= _spsb_dataImage.size.width) {
  141. [_imageView makeConstraints:^(JXHConstraintMaker *make) {
  142. make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
  143. make.size.equalTo(CGSizeMake(WIDTH, self->_showHeight + HEIGHT - WIDTH / self->_spsb_proportion));
  144. }];
  145. } else {
  146. CGFloat c = (WIDTH / _spsb_proportion - _showHeight) * _backgroundScrollView.zoomScale;
  147. [_imageView makeConstraints:^(JXHConstraintMaker *make) {
  148. make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
  149. make.size.equalTo(CGSizeMake(WIDTH, self->_showHeight + c + HEIGHT - WIDTH / self->_spsb_proportion));
  150. }];
  151. }
  152. } else {
  153. [_imageView makeConstraints:^(JXHConstraintMaker *make) {
  154. make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
  155. make.size.equalTo(CGSizeMake(WIDTH, HEIGHT));
  156. }];
  157. }
  158. }
  159. - (void)addSelectAreaView {
  160. UIView *view = [[UIView alloc] init];
  161. view.userInteractionEnabled = false;
  162. [self.view addSubview:view];
  163. [view makeConstraints:^(JXHConstraintMaker *make) {
  164. make.size.equalTo(CGSizeMake(WIDTH - jxh_onePixe() * 2, WIDTH / self->_spsb_proportion - jxh_onePixe() * 2));
  165. make.center.equalTo(CGPointMake(0, -jxh_safeInsets(spsb_keyWindow()).bottom / 2));
  166. }];
  167. [view setLayerBorderWidth:jxh_onePixe() * 2 borderColor:spsb_FFFFFF_color(1.f)];
  168. }
  169. - (void)addSelectMenuView {
  170. UIView *menuView = [[UIView alloc] init];
  171. menuView.backgroundColor = spsb_666666_5_color();
  172. [self.view addSubview:menuView];
  173. [menuView makeConstraints:^(JXHConstraintMaker *make) {
  174. make.leading.and.trailing.equalTo(0);
  175. make.bottom.equalTo(self.view.safebottom);
  176. make.height.equalTo(60);
  177. }];
  178. NSArray *titleArray = @[@"取消", @"选取"];
  179. for (NSInteger i = 0; i < 2; i ++) {
  180. UIButton *button = [UIButton convenienceWithFont:spsb_font(15) target:self action:@selector(buttonAction:)];
  181. [button setTitle:titleArray[i] titleColor:spsb_FFFFFF_color(1.f) state:JXHButtonControlStateNormal];
  182. button.tag = 2001 + i;
  183. [menuView addSubview:button];
  184. [button makeConstraints:^(JXHConstraintMaker *make) {
  185. if (i == 0) {
  186. make.leading.equalTo(0);
  187. } else {
  188. make.trailing.equalTo(0);
  189. }
  190. make.top.and.bottom.equalTo(0);
  191. make.width.equalTo(60);
  192. }];
  193. }
  194. }
  195. @end