RAPhotoPreviewController.m 8.7 KB

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