ContentPreviewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 "ContentPreviewController.h"
  9. #import "PhotoPreviewCell.h"
  10. #import "VideoPreviewCell.h"
  11. #import "iSalesDB.h"
  12. #import "AppDelegate.h"
  13. @interface ContentPreviewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  14. @property (nonatomic,assign) NSUInteger currentIndex;
  15. @property (strong, nonatomic) IBOutlet UILabel *indicator;
  16. @property (strong, nonatomic) IBOutlet UICollectionView *previewContainer;
  17. @property (nonatomic,strong) NSArray *photos;
  18. @property (nonatomic,strong) UIView *mask;
  19. @property (nonatomic,assign) BOOL hideNavigationBar;
  20. @end
  21. @implementation ContentPreviewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. // Do any additional setup after loading the view.
  25. // self.automaticallyAdjustsScrollViewInsets = NO;
  26. // self.navigationController.navigationBar.translucent = NO;
  27. // self.edgesForExtendedLayout = UIRectEdgeNone;
  28. UIView *v = [UIView new];
  29. [self.view insertSubview:v atIndex:0];
  30. NSMutableArray *tmpArr = [NSMutableArray array];
  31. int count = [[self.content objectForKey:@"count"] intValue];
  32. for (int i = 0; i < count; i++) {
  33. NSDictionary *item = [self.content objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  34. [tmpArr addObject:item];
  35. }
  36. self.photos = [tmpArr copy];
  37. self.indicator.layer.cornerRadius = 20;
  38. self.indicator.layer.masksToBounds = YES;
  39. self.previewContainer.pagingEnabled = YES;
  40. NSString *offset = [NSString stringWithFormat:@"%lu / %lu",(unsigned long)self.currentIndex + 1,(unsigned long)self.photos.count];
  41. self.indicator.text = offset;
  42. self.hideNavigationBar = self.navigationController.isNavigationBarHidden;
  43. self.navigationController.navigationBarHidden = YES;
  44. // 掩藏滚动
  45. [self.view insertSubview:self.mask belowSubview:self.indicator];
  46. }
  47. - (void)viewWillAppear:(BOOL)animated {
  48. [super viewWillAppear:animated];
  49. }
  50. - (void)viewDidLayoutSubviews {
  51. [super viewDidLayoutSubviews];
  52. // if (self.currentIndex > 0) {
  53. // [self.previewContainer scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0] atScrollPosition:16 animated:YES];
  54. // }
  55. // [self scrollToIndex:self.currentIndex + 1];
  56. [self.previewContainer setContentOffset:CGPointMake((self.currentIndex + 1) * CGRectGetWidth(self.previewContainer.frame), 0) animated:YES];
  57. }
  58. - (void)viewDidAppear:(BOOL)animated {
  59. [super viewDidAppear:animated];
  60. // [self.previewContainer scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0] atScrollPosition:16 animated:YES];
  61. __weak typeof(self) weakself = self;
  62. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  63. sleep(0.25);
  64. dispatch_async(dispatch_get_main_queue(), ^{
  65. [weakself.mask removeFromSuperview];
  66. weakself.mask = nil;
  67. });
  68. });
  69. }
  70. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  71. // 重新布局 Item 大小
  72. [self.previewContainer.collectionViewLayout invalidateLayout];
  73. // 重新布局Item位置
  74. // [self.previewContainer scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0] atScrollPosition:32 animated:NO];
  75. // [self scrollToIndex:self.currentIndex + 1];
  76. }
  77. - (BOOL)prefersStatusBarHidden {
  78. return YES;
  79. }
  80. - (void)didReceiveMemoryWarning {
  81. [super didReceiveMemoryWarning];
  82. // Dispose of any resources that can be recreated.
  83. }
  84. - (UIView *)mask {
  85. if (!_mask) {
  86. _mask = [[UIView alloc] initWithFrame:self.view.bounds];
  87. _mask.backgroundColor = [UIColor blackColor];
  88. }
  89. return _mask;
  90. }
  91. #pragma mark - Setter
  92. - (void)setOffset:(NSUInteger)offset {
  93. [self setCurrentIndex:offset];
  94. }
  95. //- (void)setCurrentIndex:(NSUInteger)currentIndex {
  96. // _currentIndex = currentIndex;
  97. // NSString *offset = [NSString stringWithFormat:@"%lu / %lu",(unsigned long)self.currentIndex + 1,(unsigned long)self.photos.count];
  98. // self.indicator.text = offset;
  99. //}
  100. #pragma mark - FlowLayout Delegate
  101. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  102. return collectionView.bounds.size;
  103. }
  104. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  105. return 0;
  106. }
  107. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  108. return 0;
  109. }
  110. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  111. return UIEdgeInsetsZero;
  112. }
  113. #pragma mark - CollectionView Delegate
  114. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  115. NSUInteger idx = [self contentOffsetForIndexPath:indexPath];
  116. NSDictionary *item = [self.photos objectAtIndex:idx];
  117. NSString *type = [item objectForKey:@"type"];
  118. if ([type isEqualToString:@"image"]) {
  119. PhotoPreviewCell *preCell = (PhotoPreviewCell *)cell;
  120. [self photoCell:preCell loadImage:item];
  121. } else if ([type isEqualToString:@"video"]) {
  122. VideoPreviewCell *videoCell = (VideoPreviewCell *)cell;
  123. videoCell.item = item;
  124. }
  125. }
  126. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  127. NSUInteger idx = [self contentOffsetForIndexPath:indexPath];
  128. NSDictionary *item = [self.photos objectAtIndex:idx];
  129. NSString *type = [item objectForKey:@"type"];
  130. if ([type isEqualToString:@"image"]) {
  131. PhotoPreviewCell *preCell = (PhotoPreviewCell *)cell;
  132. UIScrollView *sc = preCell.scrollView;
  133. sc.zoomScale = 1;
  134. sc.contentSize = CGSizeZero;
  135. sc.contentOffset = CGPointZero;
  136. } else if ([type isEqualToString:@"video"]) {
  137. VideoPreviewCell *videoCell = (VideoPreviewCell *)cell;
  138. [videoCell reset];
  139. }
  140. }
  141. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  142. NSUInteger idx = [self contentOffsetForIndexPath:indexPath];
  143. NSDictionary *item = [self.photos objectAtIndex:idx];
  144. NSString *type = [item objectForKey:@"type"];
  145. if ([type isEqualToString:@"video"]) {
  146. VideoPreviewCell *videoCell = (VideoPreviewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  147. [videoCell play];
  148. }
  149. }
  150. #pragma mark - CollectionView DataSource
  151. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  152. return self.photos.count + 2;
  153. }
  154. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  155. NSUInteger idx = [self contentOffsetForIndexPath:indexPath];
  156. NSDictionary *item = [self.photos objectAtIndex:idx];
  157. NSString *type = [item objectForKey:@"type"];
  158. if ([type isEqualToString:@"image"]) {
  159. PhotoPreviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoPreviewCell" forIndexPath:indexPath];
  160. cell.scrollView.delegate = self;
  161. return cell;
  162. } else if ([type isEqualToString:@"video"]) {
  163. VideoPreviewCell *videoCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"VideoPreviewCell" forIndexPath:indexPath];
  164. return videoCell;
  165. }
  166. return nil;
  167. }
  168. #pragma mark - ScrollView Delegate
  169. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  170. // if (scrollView == self.previewContainer) {
  171. //
  172. // CGFloat x = scrollView.contentOffset.x / scrollView.frame.size.width;
  173. // int idx = (int)x;
  174. // if (idx == x) {
  175. // if (self.currentIndex != idx) {
  176. // self.currentIndex = idx;
  177. //
  178. // }
  179. // }
  180. // }
  181. }
  182. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  183. CGFloat offsetX = scrollView.contentOffset.x;
  184. float idxf = offsetX / CGRectGetWidth(scrollView.frame);
  185. int idxi = (int)(offsetX / CGRectGetWidth(scrollView.frame));
  186. if (idxf == idxi) {
  187. if (idxi == 0) {
  188. self.currentIndex = self.photos.count - 1;
  189. } else if (idxi == self.photos.count + 1) {
  190. self.currentIndex = 0;
  191. } else {
  192. self.currentIndex = idxi - 1;
  193. }
  194. // if (self.indicator) {
  195. // self.indicator(self.currentIndex, self.photos.count);
  196. // }
  197. [self updateIndicator];
  198. } else {
  199. // idxi = ceil(idxf);
  200. // if (idxi != 0 || idxi != self.photos.count + 1) {
  201. // [self scrollToIndex:idxi];
  202. // }
  203. }
  204. if (idxi == 0) {
  205. [self scrollToIndex:self.photos.count];
  206. }
  207. if (idxi == self.photos.count + 1) {
  208. [self scrollToIndex:1];
  209. }
  210. }
  211. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  212. if (scrollView != self.previewContainer) {
  213. return scrollView.subviews.firstObject;
  214. }
  215. return nil;
  216. }
  217. - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  218. if (self.previewContainer != scrollView) {
  219. }
  220. }
  221. #pragma mark - Private
  222. - (void)updateIndicator {
  223. NSString *offset = [NSString stringWithFormat:@"%lu / %lu",(unsigned long)self.currentIndex + 1,(unsigned long)self.photos.count];
  224. self.indicator.text = offset;
  225. }
  226. - (NSUInteger)contentOffsetForIndexPath:(NSIndexPath *)indexPath {
  227. NSUInteger idx = indexPath.row;
  228. if (idx == 0) {
  229. idx = self.photos.count - 1;
  230. }else if (idx == self.photos.count + 1) {
  231. idx = 0;
  232. } else {
  233. idx = idx - 1;
  234. }
  235. return idx;
  236. }
  237. - (void)scrollToIndex:(NSUInteger)index { // 不会出现肉眼可见的滚动效果
  238. self.previewContainer.contentOffset = CGPointMake(index * CGRectGetWidth(self.previewContainer.frame), 0);
  239. }
  240. - (void) photoCell:(PhotoPreviewCell *)cell loadImage:(NSDictionary *)item_json {
  241. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  242. NSString* img_url = [item_json valueForKey:@"s"];
  243. NSString* type = item_json[@"type"];
  244. NSString* file_name=[img_url lastPathComponent];
  245. NSData* img_data=nil;
  246. // 加载Image
  247. if([type isEqualToString:@"video"])
  248. {
  249. img_data = UIImagePNGRepresentation([UIImage imageNamed:@"play"]);
  250. }
  251. else
  252. {
  253. if([item_json[@"is_localfile"] boolValue])
  254. img_data = [NSData dataWithContentsOfFile:img_url];
  255. else
  256. img_data = [iSalesDB load_cached_img:file_name loadFrom:img_url];
  257. }
  258. // 设置Image
  259. if(img_data!=nil)
  260. {
  261. dispatch_async(dispatch_get_main_queue(), ^{
  262. UIImage * img =[UIImage imageWithData:img_data];
  263. [cell setPhoto:img];
  264. });
  265. }
  266. else
  267. {
  268. NSData* downloadimg_data = nil;
  269. if (!appDelegate.offline_mode) {
  270. downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  271. }
  272. dispatch_async(dispatch_get_main_queue(), ^{
  273. if(downloadimg_data!=nil)
  274. {
  275. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  276. UIImage * img =[UIImage imageWithData:downloadimg_data];
  277. [cell setPhoto:img];
  278. }
  279. else
  280. {
  281. UIImage * img =[UIImage imageNamed:@"notfound_l"];
  282. [cell setPhoto:img];
  283. }
  284. });
  285. }
  286. }
  287. - (IBAction)closeBtnClick:(UIButton *)sender {
  288. self.navigationController.navigationBarHidden = self.hideNavigationBar;
  289. [self.navigationController popViewControllerAnimated:YES];
  290. }
  291. @end