ZFHorizontalCollectionViewController.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // ZFHorizontalCollectionViewController.m
  3. // ZFPlayer_Example
  4. //
  5. // Created by 紫枫 on 2018/11/22.
  6. // Copyright © 2018 紫枫. All rights reserved.
  7. //
  8. #import "ZFHorizontalCollectionViewController.h"
  9. #import "ZFCollectionViewCell.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. static NSString * const reuseIdentifier = @"collectionViewCell";
  16. @interface ZFHorizontalCollectionViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
  17. @property (nonatomic, strong) NSMutableArray <ZFTableData *>*dataSource;
  18. @property (nonatomic, strong) UICollectionView *collectionView;
  19. @property (nonatomic, strong) ZFPlayerController *player;
  20. @property (nonatomic, strong) ZFPlayerControlView *controlView;
  21. @property (nonatomic, strong) UILabel *markLabel;
  22. @end
  23. @implementation ZFHorizontalCollectionViewController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. self.view.backgroundColor = [UIColor whiteColor];
  27. [self.view addSubview:self.markLabel];
  28. [self.view addSubview:self.collectionView];
  29. [self requestData];
  30. /// playerManager
  31. ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
  32. /// player的tag值必须在cell里设置
  33. self.player = [ZFPlayerController playerWithScrollView:self.collectionView playerManager:playerManager containerViewTag:kPlayerViewTag];
  34. self.player.controlView = self.controlView;
  35. self.player.shouldAutoPlay = YES;
  36. self.player.disablePanMovingDirection = ZFPlayerDisablePanMovingDirectionAll;
  37. /// 1.0是消失100%时候
  38. self.player.playerDisapperaPercent = 1.0;
  39. @zf_weakify(self)
  40. self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
  41. @zf_strongify(self)
  42. if (isFullScreen) {
  43. self.player.disablePanMovingDirection = ZFPlayerDisablePanMovingDirectionNone;
  44. } else {
  45. self.player.disablePanMovingDirection = ZFPlayerDisablePanMovingDirectionAll;
  46. }
  47. };
  48. self.player.playerDidToEnd = ^(id _Nonnull asset) {
  49. @zf_strongify(self)
  50. [self.player.currentPlayerManager replay];
  51. };
  52. /// 停止的时候找出最合适的播放
  53. self.player.zf_scrollViewDidEndScrollingCallback = ^(NSIndexPath * _Nonnull indexPath) {
  54. @zf_strongify(self)
  55. [self playTheVideoAtIndexPath:indexPath];
  56. };
  57. }
  58. - (void)viewWillLayoutSubviews {
  59. [super viewWillLayoutSubviews];
  60. CGFloat width = self.view.frame.size.width;
  61. CGFloat height = width*9/16;
  62. CGFloat y = 0;
  63. self.collectionView.frame = CGRectMake(0, y, width, height);
  64. self.collectionView.center = self.view.center;
  65. self.markLabel.frame = CGRectMake(0, self.collectionView.frame.origin.y-50, 100, 20);
  66. self.markLabel.zf_centerX = self.view.zf_centerX;
  67. }
  68. - (void)viewDidAppear:(BOOL)animated {
  69. [super viewDidAppear:animated];
  70. @zf_weakify(self)
  71. [self.player zf_filterShouldPlayCellWhileScrolled:^(NSIndexPath *indexPath) {
  72. @zf_strongify(self)
  73. [self playTheVideoAtIndexPath:indexPath];
  74. }];
  75. }
  76. #pragma mark - 转屏和状态栏
  77. - (BOOL)shouldAutorotate {
  78. return NO;
  79. }
  80. - (UIStatusBarStyle)preferredStatusBarStyle {
  81. return UIStatusBarStyleDefault;
  82. }
  83. - (BOOL)prefersStatusBarHidden {
  84. return NO;
  85. }
  86. #pragma mark - private method
  87. - (void)requestData {
  88. NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
  89. NSData *data = [NSData dataWithContentsOfFile:path];
  90. NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
  91. self.dataSource = @[].mutableCopy;
  92. NSArray *videoList = [rootDict objectForKey:@"list"];
  93. for (NSDictionary *dataDic in videoList) {
  94. ZFTableData *data = [[ZFTableData alloc] init];
  95. [data setValuesForKeysWithDictionary:dataDic];
  96. [self.dataSource addObject:data];
  97. }
  98. }
  99. /// play the video
  100. - (void)playTheVideoAtIndexPath:(NSIndexPath *)indexPath {
  101. ZFTableData *data = self.dataSource[indexPath.row];
  102. [self.player playTheIndexPath:indexPath assetURL:[NSURL URLWithString:data.video_url]];
  103. [self.controlView showTitle:data.title
  104. coverURLString:data.thumbnail_url
  105. fullScreenMode:ZFFullScreenModeLandscape];
  106. }
  107. #pragma mark - UIScrollViewDelegate 列表播放必须实现
  108. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  109. [scrollView zf_scrollViewDidEndDecelerating];
  110. }
  111. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  112. [scrollView zf_scrollViewDidEndDraggingWillDecelerate:decelerate];
  113. }
  114. - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
  115. [scrollView zf_scrollViewDidScrollToTop];
  116. }
  117. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  118. [scrollView zf_scrollViewDidScroll];
  119. }
  120. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  121. [scrollView zf_scrollViewWillBeginDragging];
  122. }
  123. #pragma mark UICollectionViewDataSource
  124. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  125. return self.dataSource.count;
  126. }
  127. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  128. ZFCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
  129. cell.data = self.dataSource[indexPath.row];
  130. @zf_weakify(self)
  131. cell.playBlock = ^(UIButton *sender) {
  132. @zf_strongify(self)
  133. [self playTheVideoAtIndexPath:indexPath];
  134. };
  135. return cell;
  136. }
  137. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  138. [self playTheVideoAtIndexPath:indexPath];
  139. }
  140. - (UICollectionView *)collectionView {
  141. if (!_collectionView) {
  142. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  143. CGFloat itemWidth = self.view.frame.size.width;
  144. CGFloat itemHeight = itemWidth*9/16;
  145. layout.itemSize = CGSizeMake(itemWidth, itemHeight);
  146. layout.sectionInset = UIEdgeInsetsZero;
  147. layout.minimumLineSpacing = 0;
  148. layout.minimumInteritemSpacing = 0;
  149. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  150. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  151. _collectionView.delegate = self;
  152. _collectionView.dataSource = self;
  153. _collectionView.backgroundColor = [UIColor whiteColor];
  154. /// 横向滚动 这行代码必须写
  155. _collectionView.zf_scrollViewDirection = ZFPlayerScrollViewDirectionHorizontal;
  156. [_collectionView registerClass:[ZFCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
  157. _collectionView.pagingEnabled = YES;
  158. if (@available(iOS 11.0, *)) {
  159. _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  160. } else {
  161. self.automaticallyAdjustsScrollViewInsets = NO;
  162. }
  163. }
  164. return _collectionView;
  165. }
  166. - (ZFPlayerControlView *)controlView {
  167. if (!_controlView) {
  168. _controlView = [ZFPlayerControlView new];
  169. _controlView.fastViewAnimated = YES;
  170. _controlView.effectViewShow = NO;
  171. _controlView.customDisablePanMovingDirection = YES;
  172. }
  173. return _controlView;
  174. }
  175. - (UILabel *)markLabel {
  176. if (!_markLabel) {
  177. _markLabel = [UILabel new];
  178. _markLabel.text = @"请横向滚动";
  179. }
  180. return _markLabel;
  181. }
  182. @end