SPSBPreviewViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. //
  2. // SPSBPreviewViewController.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/18.
  6. //
  7. #import "SPSBPreviewViewController.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #import "JXHZoomImageView.h"
  10. #import "SPSBEnumerationProfile.h"
  11. #import <SDWebImage.h>
  12. #import "SPSBURLApiProfile.h"
  13. @implementation SPSBPreviewConfiguration
  14. @end
  15. @interface SPSBPreviewViewController ()<UIScrollViewDelegate, UIGestureRecognizerDelegate> {
  16. UIImageView *_animationView;
  17. UIView *_bgView;
  18. UIScrollView *_scrollView;
  19. UIPageControl *_pageControl;
  20. // BYHVideoPlayer *_player;
  21. // AVPlayerLayer *_playLayer;
  22. // UIView *_playView;
  23. NSInteger _index;
  24. SPSBPreviewConfiguration *_configuration;
  25. CGFloat _panOriginalWidth;
  26. CGFloat _panOriginalHeight;
  27. CGFloat _panOriginalX;
  28. CGFloat _panOriginalY;
  29. }
  30. @end
  31. #define spsb_offset 20
  32. #define spsb_imageViewTag 5000
  33. @implementation SPSBPreviewViewController
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. // [self initData];
  37. [self setupUI];
  38. }
  39. //- (void)initData {
  40. // _player = SPSBVideoPlayer.new;
  41. // _player.spsb_isLoop = _player.spsb_isAutoPlay = true;
  42. // _player.spsb_videoDelegate = self;
  43. //}
  44. #pragma mark - Action
  45. - (void)showWithConfiguration:(SPSBPreviewConfiguration *)configuration currentImage:(UIImage *)currentImage index:(NSInteger)index {
  46. [self.view.superview bringSubviewToFront:self.view];
  47. _index = index;
  48. _configuration = configuration;
  49. // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
  50. // _playView.hidden = false;
  51. // if (!_configuration.spsb_video) {
  52. // [self.view showToastWithTitle:SPSBLocalize(SPSBVideoPlayFailureTipsTextStringKey)];
  53. // return;
  54. // }
  55. // _pageControl.numberOfPages = 1;
  56. // _pageControl.currentPage = 0;
  57. // [_scrollView setContentSize:(CGSize){jxh_screenWidth() + spsb_offset, jxh_screenHeight()}];
  58. // [_scrollView setContentOffset: (CGPoint){0, 0}];
  59. // for (NSInteger i = 0; i < _spsb_maxPreview; i ++) {
  60. // JXHZoomImageView *imageView = [_scrollView viewWithTag:spsb_imageViewTag + i];
  61. // if (i == index) {
  62. // [imageView setImage:currentImage];
  63. // } else {
  64. // [imageView setImage:nil];
  65. // }
  66. // }
  67. // } else {
  68. // _playView.hidden = true;
  69. NSInteger count = _configuration.spsb_images.count > _spsb_maxPreview ? _spsb_maxPreview : _configuration.spsb_images.count;
  70. if (index >= count) index = count - 1;
  71. _pageControl.numberOfPages = count;
  72. _pageControl.currentPage = index;
  73. [_scrollView setContentSize:(CGSize){(jxh_screenWidth() + spsb_offset) * count, jxh_screenHeight()}];
  74. [_scrollView setContentOffset: (CGPoint){(jxh_screenWidth() + spsb_offset) * index, 0}];
  75. for (NSInteger i = 0; i < _spsb_maxPreview; i ++) {
  76. JXHZoomImageView *imageView = [_scrollView viewWithTag:spsb_imageViewTag + i];
  77. [imageView setImage:nil];
  78. if (i == index) {
  79. [imageView setImage:currentImage];
  80. } else if (i < _configuration.spsb_images.count) {
  81. @weakify(imageView)
  82. [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:spsb_fileURL(configuration.spsb_images[i]) completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
  83. if (image) {
  84. [weak_imageView setImage:image];
  85. }
  86. }];
  87. }
  88. }
  89. // }
  90. CGRect rect = [self getImageOriginalRect];
  91. _animationView.image = currentImage;
  92. [_animationView setLayerCornerRadius:configuration.spsb_cornerRadius clipToBounds:true];
  93. _animationView.frame = rect;
  94. CGRect newRect = [self getAnimationImageViewRect];
  95. self.view.backgroundColor = spsb_000000_color(1.f);
  96. self.view.hidden = _animationView.hidden = false;
  97. _spsb_previewing = true;
  98. [self setNeedsStatusBarAppearanceUpdate];
  99. [UIView animateWithDuration:0.3 animations:^{
  100. self->_animationView.frame = newRect;
  101. [self->_animationView setLayerCornerRadius:0 clipToBounds:true];
  102. } completion:^(BOOL finished) {
  103. self->_bgView.hidden = false;
  104. // if (configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
  105. // self->_playView.frame = self->_animationView.frame;
  106. // self->_playLayer = [self->_player setPlayUrl:configuration.spsb_video videoGravity:AVLayerVideoGravityResizeAspectFill layerFrame:self->_animationView.bounds];
  107. // [self->_playView.layer addSublayer:self->_playLayer];
  108. // } else {
  109. self->_animationView.hidden = true;
  110. // }
  111. }];
  112. }
  113. - (void)dismissView {
  114. JXHZoomImageView *imageView = [_scrollView viewWithTag:spsb_imageViewTag + _index];
  115. _animationView.image = imageView.jxh_imageView.image;
  116. CGRect rect = [self getAnimationImageViewRect];
  117. _animationView.frame = rect;
  118. CGRect newRect = [self getImageOriginalRect];
  119. // if (_playLayer) {
  120. // [_player playOperate:SPSBVideoOperateStop];
  121. // [_playLayer removeFromSuperlayer];
  122. // _playLayer = nil;
  123. // }
  124. _bgView.hidden = true;
  125. _animationView.hidden = false;
  126. _spsb_previewing = false;
  127. [UIView animateWithDuration:0.3 animations:^{
  128. self->_animationView.frame = newRect;
  129. [self->_animationView setLayerCornerRadius:self->_configuration.spsb_cornerRadius clipToBounds:true];
  130. [self setNeedsStatusBarAppearanceUpdate];
  131. self.view.backgroundColor = spsb_000000_color(0.f);
  132. } completion:^(BOOL finished) {
  133. self->_animationView.image = nil;
  134. self.view.hidden = self->_animationView.hidden = true;
  135. }];
  136. }
  137. - (void)dismissViewPan {
  138. // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
  139. // _animationView.frame = _playView.frame;
  140. // }
  141. CGRect newRect = [self getImageOriginalRect];
  142. // if (_playLayer) {
  143. // [_player playOperate:SPSBVideoOperateStop];
  144. // [_playLayer removeFromSuperlayer];
  145. // _playLayer = nil;
  146. // }
  147. _bgView.hidden = true;
  148. _animationView.hidden = false;
  149. _spsb_previewing = false;
  150. [UIView animateWithDuration:0.3 animations:^{
  151. self->_animationView.frame = newRect;
  152. [self->_animationView setLayerCornerRadius:self->_configuration.spsb_cornerRadius clipToBounds:true];
  153. [self setNeedsStatusBarAppearanceUpdate];
  154. self.view.backgroundColor = spsb_000000_color(0.f);
  155. } completion:^(BOOL finished) {
  156. self->_animationView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
  157. // self->_playView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
  158. self->_animationView.image = nil;
  159. self.view.hidden = self->_animationView.hidden = true;
  160. }];
  161. }
  162. - (CGRect)getAnimationImageViewRect {
  163. UIImage *image = _animationView.image;
  164. if (!image) {
  165. return (CGRect){0, 0, jxh_screenWidth(), jxh_screenHeight()};
  166. }
  167. // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
  168. // CGFloat proportion = jxh_viewHeight(self.view) / jxh_viewWidth(self.view) - image.size.height / image.size.width;
  169. // if (proportion > 0 && proportion < SPSBVideoFullScreenDifferentProportion) {
  170. // CGFloat width = image.size.width / image.size.height * jxh_screenHeight();
  171. // CGFloat x = (jxh_screenWidth() - width) / 2;
  172. // return (CGRect){x, 0, width, jxh_screenHeight()};
  173. // } else {
  174. // CGFloat height = image.size.height / image.size.width * jxh_screenWidth();
  175. // CGFloat y = (jxh_screenHeight() - height) / 2;
  176. // return (CGRect){0, y, jxh_screenWidth(), height};
  177. // }
  178. // } else {
  179. CGFloat height = image.size.height / image.size.width * jxh_screenWidth();
  180. CGFloat y = (jxh_screenHeight() - height) / 2;
  181. if (y < 0) y = 0;
  182. return (CGRect){0, y, jxh_screenWidth(), height};
  183. // }
  184. }
  185. - (CGRect)getAnimationImageViewPanRectWithView:(JXHZoomImageView *)view {
  186. UIImage *image = _animationView.image;
  187. if (!image) {
  188. return (CGRect){0, 0, jxh_screenWidth(), jxh_screenHeight()};
  189. }
  190. // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
  191. // CGFloat proportion = jxh_viewHeight(self.view) / jxh_viewWidth(self.view) - image.size.height / image.size.width;
  192. // if (proportion > 0 && proportion < SPSBVideoFullScreenDifferentProportion) {
  193. // CGFloat width = image.size.width / image.size.height * jxh_screenHeight();
  194. // CGFloat x = (jxh_screenWidth() - width) / 2;
  195. // return (CGRect){x, 0, width, jxh_screenHeight()};
  196. // } else {
  197. // CGFloat height = image.size.height / image.size.width * jxh_screenWidth();
  198. // CGFloat y = (jxh_screenHeight() - height) / 2;
  199. // return (CGRect){0, y, jxh_screenWidth(), height};
  200. // }
  201. // } else {
  202. CGFloat height = image.size.height / image.size.width * jxh_viewWidth(view.jxh_imageView);
  203. CGFloat y = (jxh_screenHeight() - height) / 2;
  204. if (y < 0) y = 0;
  205. return (CGRect){-view.contentOffset.x, y, jxh_viewWidth(view.jxh_imageView), height};
  206. // }
  207. }
  208. - (CGRect)getImageOriginalRect {
  209. if (_spsb_getOriginalRect) {
  210. return _spsb_getOriginalRect(_index);
  211. } else {
  212. return CGRectZero;
  213. }
  214. }
  215. - (void)panAction:(UIPanGestureRecognizer *)sender {
  216. // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo && !_player) {
  217. // sender.state = UIGestureRecognizerStateFailed;
  218. // return;
  219. // }
  220. CGPoint p = [sender translationInView:self.view];
  221. JXHZoomImageView *imageView = [_scrollView viewWithTag:spsb_imageViewTag + _index];
  222. if (!imageView.jxh_imageView.image) {
  223. sender.state = UIGestureRecognizerStateFailed;
  224. return;
  225. }
  226. CGFloat scale = 1 - p.y / jxh_viewHeight(self.view) / 2;
  227. if (scale > 1) {
  228. scale = 1.f;
  229. }
  230. switch (sender.state) {
  231. case UIGestureRecognizerStateBegan: {
  232. if ([sender translationInView:self.view].x != 0) {
  233. sender.state = UIGestureRecognizerStateFailed;
  234. return;
  235. }
  236. if (imageView.contentOffset.y + imageView.contentInset.top > 1) {
  237. sender.state = UIGestureRecognizerStateFailed;
  238. return;
  239. }
  240. if (p.y <= 0) {
  241. sender.state = UIGestureRecognizerStateFailed;
  242. return;
  243. }
  244. _scrollView.panGestureRecognizer.state = UIGestureRecognizerStateFailed;
  245. imageView.panGestureRecognizer.state = UIGestureRecognizerStateFailed;
  246. // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
  247. // _bgView.hidden = true;
  248. // _animationView.hidden = true;
  249. // [_player playOperate:SPSBVideoOperatePause];
  250. // _panOriginalWidth = jxh_viewWidth(_playView);
  251. // _panOriginalHeight = jxh_viewHeight(_playView);
  252. // CGPoint location = [sender locationInView:_playView];
  253. // _panOriginalX = location.x;
  254. // _panOriginalY = location.y;
  255. // _playView.transform = CGAffineTransformMake(scale, 0, 0, scale, p.x, p.y);
  256. // } else {
  257. _animationView.image = imageView.jxh_imageView.image;
  258. _animationView.frame = [self getAnimationImageViewPanRectWithView:imageView];
  259. _panOriginalWidth = jxh_viewWidth(_animationView);
  260. _panOriginalHeight = jxh_viewHeight(_animationView);
  261. CGPoint location = [sender locationInView:_animationView];
  262. _panOriginalX = location.x;
  263. _panOriginalY = location.y;
  264. debugLog(@"%lf----%lf", location.x, location.y);
  265. _animationView.hidden = false;
  266. _bgView.hidden = true;
  267. _animationView.transform = CGAffineTransformMake(scale, 0, 0, scale, p.x, p.y);
  268. // }
  269. self.view.backgroundColor = spsb_000000_color(scale);
  270. }
  271. break;
  272. case UIGestureRecognizerStateChanged: {
  273. CGFloat change = (1 - scale);
  274. CGFloat y = p.y - (_panOriginalHeight / 2 - _panOriginalY) * change;
  275. CGFloat x = p.x - (_panOriginalWidth / 2 - _panOriginalX) * change;
  276. // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
  277. // _playView.transform = CGAffineTransformMake(scale, 0, 0, scale, x, y);
  278. // } else {
  279. _animationView.transform = CGAffineTransformMake(scale, 0, 0, scale, x, y);
  280. // }
  281. self.view.backgroundColor = spsb_000000_color(scale);
  282. }
  283. break;
  284. case UIGestureRecognizerStateFailed:
  285. // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
  286. // _playView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
  287. // _bgView.hidden = false;
  288. // [_player playOperate:SPSBVideoOperatePlay];
  289. // } else {
  290. _animationView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
  291. _animationView.hidden = true;
  292. _bgView.hidden = false;
  293. // }
  294. self.view.backgroundColor = spsb_000000_color(1.f);
  295. break;
  296. default:
  297. if ([sender velocityInView:self.view].y > 1000) {
  298. [self dismissViewPan];
  299. return;
  300. }
  301. if (p.y > jxh_viewHeight(self.view) / 2) {
  302. [self dismissViewPan];
  303. return;
  304. }
  305. // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
  306. // [UIView animateWithDuration:0.3 animations:^{
  307. // self->_playView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
  308. // self.view.backgroundColor = spsb_000000_color(1.f);
  309. // } completion:^(BOOL finished) {
  310. // self->_bgView.hidden = false;
  311. // [self->_player playOperate:SPSBVideoOperatePlay];
  312. // }];
  313. // } else {
  314. [UIView animateWithDuration:0.3 animations:^{
  315. self->_animationView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
  316. self.view.backgroundColor = spsb_000000_color(1.f);
  317. } completion:^(BOOL finished) {
  318. self->_animationView.hidden = true;
  319. self->_bgView.hidden = false;
  320. }];
  321. // }
  322. break;
  323. }
  324. }
  325. //#pragma mark - SPSBNotificationDelegate
  326. //- (void)spsb_shouldEnterRoom {
  327. // [self dismissView];
  328. //}
  329. #pragma mark - UIScrollViewDelegate
  330. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  331. _pageControl.currentPage = scrollView.contentOffset.x / (jxh_screenWidth() + spsb_offset);
  332. }
  333. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  334. if (scrollView != _scrollView) return;
  335. if (!decelerate) {
  336. [self endScroll:scrollView];
  337. }
  338. }
  339. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  340. if (scrollView != _scrollView) return;
  341. [self endScroll:scrollView];
  342. }
  343. - (void)endScroll:(UIScrollView *)scrollView {
  344. _index = scrollView.contentOffset.x / (jxh_screenWidth() + spsb_offset);
  345. _pageControl.currentPage = _index;
  346. }
  347. //#pragma mark - SPSBVideoPlayerDelegate
  348. //- (void)spsb_videoPlayFailed:(NSError *)error {
  349. // [self.view showToastWithTitle:SPSBLocalize(SPSBVideoPlayFailureTipsTextStringKey)];
  350. // [self dismissView];
  351. //}
  352. //
  353. //- (void)spsb_videoPlayStatusChanged:(SPSBVideoStatus)status {
  354. //
  355. //}
  356. #pragma mark - UIGestureRecognizerDelegate
  357. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  358. if ([otherGestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
  359. return false;
  360. }
  361. return true;
  362. }
  363. #pragma mark - UI
  364. - (void)setupUI {
  365. self.view.backgroundColor = spsb_000000_color(0.f);
  366. self.view.hidden = true;
  367. [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)]];
  368. UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
  369. panGes.delegate = self;
  370. [self.view addGestureRecognizer:panGes];
  371. [self createBgView];
  372. _animationView = UIImageView.new;
  373. _animationView.clipsToBounds = true;
  374. _animationView.contentMode = UIViewContentModeScaleAspectFill;
  375. [self.view addSubview:_animationView];
  376. // _playView = UIView.new;
  377. // [self.view addSubview:_playView];
  378. }
  379. - (void)createBgView {
  380. _bgView = UIView.new;
  381. _bgView.hidden = true;
  382. [self.view addSubview:_bgView];
  383. [_bgView makeConstraints:^(JXHConstraintMaker *make) {
  384. make.edges.equalTo(self.view);
  385. }];
  386. _scrollView = [UIScrollView convenienceWithBackgroundColor:spsb_clear_color()];
  387. _scrollView.pagingEnabled = true;
  388. _scrollView.delegate = self;
  389. [_bgView addSubview:_scrollView];
  390. [_scrollView makeConstraints:^(JXHConstraintMaker *make) {
  391. make.edges.equalTo((UIEdgeInsets){0, 0, 0, -spsb_offset});
  392. }];
  393. for (NSInteger i = 0; i < _spsb_maxPreview; i ++) {
  394. JXHZoomImageView *imageView = [[JXHZoomImageView alloc] initWithTarget:self action:@selector(dismissView)];
  395. imageView.tag = spsb_imageViewTag + i;
  396. [_scrollView addSubview:imageView];
  397. [imageView makeConstraints:^(JXHConstraintMaker *make) {
  398. make.leading.equalTo(i * (jxh_screenWidth() + spsb_offset));
  399. make.top.and.bottom.equalTo(0);
  400. make.size.equalTo((CGSize){jxh_screenWidth(), jxh_screenHeight()});
  401. }];
  402. }
  403. _pageControl = UIPageControl.new;
  404. _pageControl.hidesForSinglePage = true;
  405. [_bgView addSubview:_pageControl];
  406. [_pageControl makeConstraints:^(JXHConstraintMaker *make) {
  407. make.bottom.equalTo(-10);
  408. make.centerX.equalTo(self->_bgView);
  409. }];
  410. }
  411. @end
  412. #undef spsb_offset
  413. #undef spsb_imageViewTag