RAPhotoPreviewController.m 7.8 KB

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