RAPhotoPreviewController.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  62. // 重新布局 Item 大小
  63. [self.previewContainer.collectionViewLayout invalidateLayout];
  64. }
  65. - (BOOL)prefersStatusBarHidden {
  66. return YES;
  67. }
  68. - (void)didReceiveMemoryWarning {
  69. [super didReceiveMemoryWarning];
  70. // Dispose of any resources that can be recreated.
  71. }
  72. #pragma mark - Setter
  73. - (void)setOffset:(NSUInteger)offset {
  74. [self setCurrentIndex:offset];
  75. }
  76. #pragma mark - FlowLayout Delegate
  77. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  78. return collectionView.bounds.size;
  79. }
  80. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  81. return 0;
  82. }
  83. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  84. return 0;
  85. }
  86. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  87. return UIEdgeInsetsZero;
  88. }
  89. #pragma mark - CollectionView Delegate
  90. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  91. NSUInteger idx = [self contentOffsetForIndexPath:indexPath];
  92. PhotoPreviewCell *preCell = (PhotoPreviewCell *)cell;
  93. id<RAPhotoItemDelegate> model = [self.photos objectAtIndex:idx];
  94. preCell.model = model;
  95. }
  96. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  97. PhotoPreviewCell *preCell = (PhotoPreviewCell *)cell;
  98. [preCell reset];
  99. }
  100. #pragma mark - CollectionView DataSource
  101. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  102. if ([self.photos count] == 0) {
  103. return 0;
  104. }
  105. return self.photos.count + 2;
  106. }
  107. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  108. PhotoPreviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoPreviewCell" forIndexPath:indexPath];
  109. cell.scrollView.delegate = self;
  110. return cell;
  111. }
  112. #pragma mark - ScrollView Delegate
  113. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  114. if (scrollView == self.previewContainer) {
  115. CGFloat offsetX = scrollView.contentOffset.x;
  116. float idxf = offsetX / CGRectGetWidth(scrollView.frame);
  117. int idxi = (int)(offsetX / CGRectGetWidth(scrollView.frame));
  118. if (idxf == idxi) {
  119. if (idxi == 0) {
  120. self.currentIndex = self.photos.count - 1;
  121. } else if (idxi == self.photos.count + 1) {
  122. self.currentIndex = 0;
  123. } else {
  124. self.currentIndex = idxi - 1;
  125. }
  126. [self updateIndicator];
  127. } else {
  128. }
  129. if (idxi == 0) {
  130. [self scrollToIndex:self.photos.count];
  131. }
  132. if (idxi == self.photos.count + 1) {
  133. [self scrollToIndex:1];
  134. }
  135. }
  136. }
  137. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  138. if (scrollView != self.previewContainer) {
  139. return scrollView.subviews.firstObject;
  140. }
  141. return nil;
  142. }
  143. - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  144. if (self.previewContainer != scrollView) {
  145. }
  146. }
  147. #pragma mark - Private
  148. - (void)updateIndicator {
  149. NSString *offset = [NSString stringWithFormat:@"%lu / %lu",(unsigned long)self.currentIndex + 1,(unsigned long)self.photos.count];
  150. self.indicator.text = offset;
  151. }
  152. - (NSUInteger)contentOffsetForIndexPath:(NSIndexPath *)indexPath {
  153. NSUInteger idx = indexPath.row;
  154. if (idx == 0) {
  155. idx = self.photos.count - 1;
  156. }else if (idx == self.photos.count + 1) {
  157. idx = 0;
  158. } else {
  159. idx = idx - 1;
  160. }
  161. return idx;
  162. }
  163. - (void)scrollToIndex:(NSUInteger)index { // 不会出现肉眼可见的滚动效果
  164. self.previewContainer.contentOffset = CGPointMake(index * CGRectGetWidth(self.previewContainer.frame), 0);
  165. }
  166. - (IBAction)closeBtnClick:(UIButton *)sender {
  167. if (self.navigationController) {
  168. self.navigationController.navigationBarHidden = self.hideNavigationBar;
  169. [self.navigationController popViewControllerAnimated:YES];
  170. } else {
  171. [self dismissViewControllerAnimated:YES completion:nil];
  172. }
  173. }
  174. @end