RAPhotoPreviewController.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // NewPhotoPreviewController.m
  3. // RA Image
  4. //
  5. // Created by Jack on 2017/6/14.
  6. // Copyright © 2017年 USAI. All rights reserved.
  7. //
  8. #import "RAPhotoPreviewController.h"
  9. #import "PhotoPreviewCell.h"
  10. @interface RAPhotoPreviewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  11. @property (nonatomic,assign) NSUInteger currentIndex;
  12. @property (strong, nonatomic) IBOutlet UILabel *indicator;
  13. @property (strong, nonatomic) IBOutlet UICollectionView *previewContainer;
  14. @property (nonatomic,strong) NSArray<id<RAPhotoItemDelegate>> *photos;
  15. @property (nonatomic,assign) NSUInteger offset;
  16. @property (nonatomic,assign) BOOL hideNavigationBar;
  17. @end
  18. @implementation RAPhotoPreviewController
  19. + (instancetype)ra_photoPreviewControllerWithPhotoItems:(NSArray<id<RAPhotoItemDelegate>> *)items offset:(NSUInteger)offset {
  20. RAPhotoPreviewController *vc = [[UIStoryboard storyboardWithName:@"PhotoList" bundle:nil] instantiateViewControllerWithIdentifier:@"RAPhotoPreviewController"];
  21. if (offset == 0 || offset >= items.count) {
  22. vc.photos = items;
  23. } else {
  24. NSMutableArray<id<RAPhotoItemDelegate>> *tmpArr = [NSMutableArray array];
  25. [tmpArr addObjectsFromArray:[items subarrayWithRange:NSMakeRange(offset, items.count - offset)]];
  26. for (int i = 0; i < offset; i++) {
  27. id<RAPhotoItemDelegate> model = [items objectAtIndex:i];
  28. [tmpArr addObject:model];
  29. }
  30. items = [tmpArr copy];
  31. vc.photos = items;
  32. }
  33. vc.offset = 0;
  34. return vc;
  35. }
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. if (@available(iOS 11, *)) {
  39. self.previewContainer.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  40. } else {
  41. self.automaticallyAdjustsScrollViewInsets = NO;
  42. }
  43. self.indicator.layer.cornerRadius = 20;
  44. self.indicator.layer.masksToBounds = YES;
  45. self.previewContainer.pagingEnabled = YES;
  46. NSString *offset = [NSString stringWithFormat:@"%lu / %lu",(unsigned long)self.currentIndex + 1,(unsigned long)self.photos.count];
  47. self.indicator.text = offset;
  48. self.hideNavigationBar = self.navigationController.isNavigationBarHidden;
  49. self.navigationController.navigationBarHidden = YES;
  50. }
  51. - (void)viewWillAppear:(BOOL)animated {
  52. [super viewWillAppear:animated];
  53. }
  54. - (void)viewDidLayoutSubviews {
  55. [super viewDidLayoutSubviews];
  56. [self.previewContainer setContentOffset:CGPointMake((self.currentIndex + 1) * CGRectGetWidth(self.previewContainer.frame), 0) animated:NO];
  57. }
  58. - (void)viewDidAppear:(BOOL)animated {
  59. [super viewDidAppear:animated];
  60. }
  61. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  62. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  63. [coordinator animateAlongsideTransitionInView:self.previewContainer animation:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  64. // 重新布局 Item 大小
  65. [self.previewContainer.collectionViewLayout invalidateLayout];
  66. } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  67. [self.previewContainer reloadData];
  68. }];
  69. }
  70. - (BOOL)prefersStatusBarHidden {
  71. return YES;
  72. }
  73. - (void)didReceiveMemoryWarning {
  74. [super didReceiveMemoryWarning];
  75. // Dispose of any resources that can be recreated.
  76. }
  77. #pragma mark - Setter
  78. - (void)setOffset:(NSUInteger)offset {
  79. [self setCurrentIndex:offset];
  80. }
  81. #pragma mark - FlowLayout Delegate
  82. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  83. return collectionView.bounds.size;
  84. }
  85. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  86. return 0;
  87. }
  88. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  89. return 0;
  90. }
  91. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  92. return UIEdgeInsetsZero;
  93. }
  94. #pragma mark - CollectionView Delegate
  95. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  96. NSUInteger idx = [self contentOffsetForIndexPath:indexPath];
  97. PhotoPreviewCell *preCell = (PhotoPreviewCell *)cell;
  98. id<RAPhotoItemDelegate> model = [self.photos objectAtIndex:idx];
  99. preCell.model = model;
  100. }
  101. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  102. PhotoPreviewCell *preCell = (PhotoPreviewCell *)cell;
  103. [preCell reset];
  104. }
  105. #pragma mark - CollectionView DataSource
  106. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  107. if ([self.photos count] == 0) {
  108. return 0;
  109. }
  110. return self.photos.count + 2;
  111. }
  112. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  113. PhotoPreviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoPreviewCell" forIndexPath:indexPath];
  114. cell.scrollView.delegate = self;
  115. return cell;
  116. }
  117. #pragma mark - ScrollView Delegate
  118. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  119. if (scrollView == self.previewContainer) {
  120. CGFloat offsetX = scrollView.contentOffset.x;
  121. float idxf = offsetX / CGRectGetWidth(scrollView.frame);
  122. int idxi = (int)(offsetX / CGRectGetWidth(scrollView.frame));
  123. if (idxf == idxi) {
  124. if (idxi == 0) {
  125. self.currentIndex = self.photos.count - 1;
  126. } else if (idxi == self.photos.count + 1) {
  127. self.currentIndex = 0;
  128. } else {
  129. self.currentIndex = idxi - 1;
  130. }
  131. [self updateIndicator];
  132. } else {
  133. }
  134. if (idxi == 0) {
  135. [self scrollToIndex:self.photos.count];
  136. }
  137. if (idxi == self.photos.count + 1) {
  138. [self scrollToIndex:1];
  139. }
  140. }
  141. }
  142. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  143. if (scrollView != self.previewContainer) {
  144. return scrollView.subviews.firstObject;
  145. }
  146. return nil;
  147. }
  148. - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  149. if (self.previewContainer != scrollView) {
  150. }
  151. }
  152. #pragma mark - Private
  153. - (void)updateIndicator {
  154. NSString *offset = [NSString stringWithFormat:@"%lu / %lu",(unsigned long)self.currentIndex + 1,(unsigned long)self.photos.count];
  155. self.indicator.text = offset;
  156. }
  157. - (NSUInteger)contentOffsetForIndexPath:(NSIndexPath *)indexPath {
  158. NSUInteger idx = indexPath.row;
  159. if (idx == 0) {
  160. idx = self.photos.count - 1;
  161. }else if (idx == self.photos.count + 1) {
  162. idx = 0;
  163. } else {
  164. idx = idx - 1;
  165. }
  166. return idx;
  167. }
  168. - (void)scrollToIndex:(NSUInteger)index { // 不会出现肉眼可见的滚动效果
  169. self.previewContainer.contentOffset = CGPointMake(index * CGRectGetWidth(self.previewContainer.frame), 0);
  170. }
  171. - (IBAction)closeBtnClick:(UIButton *)sender {
  172. if (self.navigationController) {
  173. self.navigationController.navigationBarHidden = self.hideNavigationBar;
  174. [self.navigationController popViewControllerAnimated:YES];
  175. } else {
  176. [self dismissViewControllerAnimated:YES completion:nil];
  177. }
  178. }
  179. @end