RAPhotoPreviewController.m 8.9 KB

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