ZFDouyinCollectionViewController.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // ZFDouyinCollectionViewController.m
  3. // ZFPlayer_Example
  4. //
  5. // Created by 紫枫 on 2019/6/4.
  6. // Copyright © 2019 紫枫. All rights reserved.
  7. //
  8. #import "ZFDouyinCollectionViewController.h"
  9. #import "ZFDouyinCollectionViewCell.h"
  10. #import "ZFTableData.h"
  11. #import <ZFPlayer/ZFAVPlayerManager.h>
  12. #import <ZFPlayer/ZFPlayerControlView.h>
  13. #import <ZFPlayer/UIView+ZFFrame.h>
  14. #import <ZFPlayer/ZFPlayerConst.h>
  15. #import "UINavigationController+FDFullscreenPopGesture.h"
  16. #import "ZFDouYinControlView.h"
  17. #import "ZFDouYinCellDelegate.h"
  18. #import "ZFCustomControlView.h"
  19. static NSString * const reuseIdentifier = @"collectionViewCell";
  20. @interface ZFDouyinCollectionViewController () <UICollectionViewDelegate,UICollectionViewDataSource,ZFDouYinCellDelegate>
  21. @property (nonatomic, strong) NSMutableArray <ZFTableData *>*dataSource;
  22. @property (nonatomic, strong) UICollectionView *collectionView;
  23. @property (nonatomic, strong) ZFPlayerController *player;
  24. @property (nonatomic, strong) ZFDouYinControlView *controlView;
  25. @property (nonatomic, strong) UIButton *backBtn;
  26. @property (nonatomic, strong) ZFCustomControlView *fullControlView;
  27. @end
  28. @implementation ZFDouyinCollectionViewController
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. self.fd_prefersNavigationBarHidden = YES;
  32. [self.view addSubview:self.collectionView];
  33. [self.view addSubview:self.backBtn];
  34. [self requestData];
  35. /// playerManager
  36. ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
  37. /// player的tag值必须在cell里设置
  38. self.player = [ZFPlayerController playerWithScrollView:self.collectionView playerManager:playerManager containerViewTag:kPlayerViewTag];
  39. self.player.controlView = self.controlView;
  40. self.player.shouldAutoPlay = YES;
  41. self.player.allowOrentitaionRotation = NO;
  42. self.player.disablePanMovingDirection = ZFPlayerDisablePanMovingDirectionAll;
  43. /// 1.0是消失100%时候
  44. self.player.playerDisapperaPercent = 1.0;
  45. @zf_weakify(self)
  46. self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
  47. @zf_strongify(self)
  48. if (isFullScreen) {
  49. self.player.disablePanMovingDirection = ZFPlayerDisablePanMovingDirectionNone;
  50. } else {
  51. self.player.disablePanMovingDirection = ZFPlayerDisablePanMovingDirectionAll;
  52. }
  53. self.player.controlView.hidden = YES;
  54. };
  55. self.player.orientationDidChanged = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
  56. @zf_strongify(self)
  57. self.player.controlView.hidden = NO;
  58. if (isFullScreen) {
  59. self.player.controlView = self.fullControlView;
  60. } else {
  61. self.player.controlView = self.controlView;
  62. }
  63. };
  64. self.player.playerDidToEnd = ^(id _Nonnull asset) {
  65. @zf_strongify(self)
  66. [self.player.currentPlayerManager replay];
  67. };
  68. /// 停止的时候找出最合适的播放
  69. self.player.zf_scrollViewDidEndScrollingCallback = ^(NSIndexPath * _Nonnull indexPath) {
  70. @zf_strongify(self)
  71. [self playTheVideoAtIndexPath:indexPath];
  72. };
  73. }
  74. - (void)viewWillLayoutSubviews {
  75. [super viewWillLayoutSubviews];
  76. self.backBtn.frame = CGRectMake(15, CGRectGetMaxY([UIApplication sharedApplication].statusBarFrame), 36, 36);
  77. }
  78. - (void)viewDidAppear:(BOOL)animated {
  79. [super viewDidAppear:animated];
  80. @zf_weakify(self)
  81. [self.player zf_filterShouldPlayCellWhileScrolled:^(NSIndexPath *indexPath) {
  82. @zf_strongify(self)
  83. [self playTheVideoAtIndexPath:indexPath];
  84. }];
  85. }
  86. #pragma mark - 转屏和状态栏
  87. - (BOOL)shouldAutorotate {
  88. return NO;
  89. }
  90. - (UIStatusBarStyle)preferredStatusBarStyle {
  91. return UIStatusBarStyleLightContent;
  92. }
  93. - (BOOL)prefersStatusBarHidden {
  94. return NO;
  95. }
  96. #pragma mark - private method
  97. - (void)requestData {
  98. NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
  99. NSData *data = [NSData dataWithContentsOfFile:path];
  100. NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
  101. self.dataSource = @[].mutableCopy;
  102. NSArray *videoList = [rootDict objectForKey:@"list"];
  103. for (NSDictionary *dataDic in videoList) {
  104. ZFTableData *data = [[ZFTableData alloc] init];
  105. [data setValuesForKeysWithDictionary:dataDic];
  106. [self.dataSource addObject:data];
  107. }
  108. }
  109. - (void)playTheIndex:(NSInteger)index {
  110. @zf_weakify(self)
  111. /// 指定到某一行播放
  112. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
  113. [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  114. [self.player zf_filterShouldPlayCellWhileScrolled:^(NSIndexPath *indexPath) {
  115. @zf_strongify(self)
  116. [self playTheVideoAtIndexPath:indexPath];
  117. }];
  118. /// 如果是最后一行,去请求新数据
  119. if (index == self.dataSource.count-1) {
  120. /// 加载下一页数据
  121. [self requestData];
  122. [self.collectionView reloadData];
  123. }
  124. }
  125. /// play the video
  126. - (void)playTheVideoAtIndexPath:(NSIndexPath *)indexPath {
  127. ZFTableData *data = self.dataSource[indexPath.row];
  128. [self.player playTheIndexPath:indexPath assetURL:[NSURL URLWithString:data.video_url]];
  129. [self.controlView resetControlView];
  130. [self.controlView showCoverViewWithUrl:data.thumbnail_url];
  131. [self.fullControlView showTitle:@"custom landscape controlView" coverURLString:data.thumbnail_url fullScreenMode:ZFFullScreenModeLandscape];
  132. }
  133. - (void)backClick:(UIButton *)sender {
  134. [self.navigationController popViewControllerAnimated:YES];
  135. }
  136. #pragma mark - UIScrollViewDelegate 列表播放必须实现
  137. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  138. [scrollView zf_scrollViewDidEndDecelerating];
  139. }
  140. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  141. [scrollView zf_scrollViewDidEndDraggingWillDecelerate:decelerate];
  142. }
  143. - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
  144. [scrollView zf_scrollViewDidScrollToTop];
  145. }
  146. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  147. [scrollView zf_scrollViewDidScroll];
  148. }
  149. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  150. [scrollView zf_scrollViewWillBeginDragging];
  151. }
  152. #pragma mark - ZFDouYinCellDelegate
  153. - (void)zf_douyinRotation {
  154. UIInterfaceOrientation orientation = UIInterfaceOrientationUnknown;
  155. if (self.player.isFullScreen) {
  156. orientation = UIInterfaceOrientationPortrait;
  157. } else {
  158. orientation = UIInterfaceOrientationLandscapeRight;
  159. }
  160. [self.player rotateToOrientation:orientation animated:YES completion:nil];
  161. }
  162. #pragma mark UICollectionViewDataSource
  163. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  164. return self.dataSource.count;
  165. }
  166. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  167. ZFDouyinCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
  168. cell.data = self.dataSource[indexPath.row];
  169. cell.delegate = self;
  170. return cell;
  171. }
  172. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  173. [self playTheVideoAtIndexPath:indexPath];
  174. }
  175. - (UICollectionView *)collectionView {
  176. if (!_collectionView) {
  177. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  178. CGFloat itemWidth = self.view.frame.size.width;
  179. CGFloat itemHeight = self.view.frame.size.height;
  180. layout.itemSize = CGSizeMake(itemWidth, itemHeight);
  181. layout.sectionInset = UIEdgeInsetsZero;
  182. layout.minimumLineSpacing = 0;
  183. layout.minimumInteritemSpacing = 0;
  184. if (self.scrollViewDirection == ZFPlayerScrollViewDirectionVertical) {
  185. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  186. } else if (self.scrollViewDirection == ZFPlayerScrollViewDirectionHorizontal) {
  187. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  188. }
  189. _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
  190. _collectionView.delegate = self;
  191. _collectionView.dataSource = self;
  192. _collectionView.backgroundColor = [UIColor whiteColor];
  193. /// 横向滚动 这行代码必须写
  194. _collectionView.zf_scrollViewDirection = self.scrollViewDirection;
  195. [_collectionView registerClass:[ZFDouyinCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
  196. _collectionView.pagingEnabled = YES;
  197. _collectionView.showsVerticalScrollIndicator = NO;
  198. _collectionView.showsHorizontalScrollIndicator = NO;
  199. _collectionView.scrollsToTop = NO;
  200. if (@available(iOS 11.0, *)) {
  201. _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  202. } else {
  203. self.automaticallyAdjustsScrollViewInsets = NO;
  204. }
  205. }
  206. return _collectionView;
  207. }
  208. - (ZFDouYinControlView *)controlView {
  209. if (!_controlView) {
  210. _controlView = [ZFDouYinControlView new];
  211. }
  212. return _controlView;
  213. }
  214. - (ZFCustomControlView *)fullControlView {
  215. if (!_fullControlView) {
  216. _fullControlView = [[ZFCustomControlView alloc] init];
  217. }
  218. return _fullControlView;
  219. }
  220. - (UIButton *)backBtn {
  221. if (!_backBtn) {
  222. _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  223. [_backBtn setImage:[UIImage imageNamed:@"icon_titlebar_whiteback"] forState:UIControlStateNormal];
  224. [_backBtn addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
  225. }
  226. return _backBtn;
  227. }
  228. @end