ZFTableHeaderViewController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // ZFTableHeaderViewController.m
  3. // ZFPlayer_Example
  4. //
  5. // Created by 紫枫 on 2018/10/30.
  6. // Copyright © 2018 紫枫. All rights reserved.
  7. //
  8. #import "ZFTableHeaderViewController.h"
  9. #import "ZFTableHeaderView.h"
  10. #import <ZFPlayer/ZFAVPlayerManager.h>
  11. #import <ZFPlayer/ZFPlayerControlView.h>
  12. #import <ZFPlayer/ZFIJKPlayerManager.h>
  13. #import <ZFPlayer/UIView+ZFFrame.h>
  14. #import <ZFPlayer/ZFPlayerConst.h>
  15. #import "ZFPlayerDetailViewController.h"
  16. #import "ZFTableData.h"
  17. #import "ZFOtherCell.h"
  18. #import "ZFUtilities.h"
  19. static NSString *kIdentifier = @"kIdentifier";
  20. @interface ZFTableHeaderViewController () <UITableViewDelegate,UITableViewDataSource>
  21. @property (nonatomic, strong) UITableView *tableView;
  22. @property (nonatomic, strong) ZFTableHeaderView *headerView;
  23. @property (nonatomic, strong) ZFPlayerController *player;
  24. @property (nonatomic, strong) ZFPlayerControlView *controlView;
  25. @property (nonatomic, strong) NSMutableArray *dataSource;
  26. @end
  27. @implementation ZFTableHeaderViewController
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. self.view.backgroundColor = [UIColor whiteColor];
  31. [self.view addSubview:self.tableView];
  32. [self requestData];
  33. self.tableView.tableHeaderView = self.headerView;
  34. self.headerView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width*9/16);
  35. /// playerManager
  36. ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
  37. /// player的tag值必须在cell里设置
  38. self.player = [ZFPlayerController playerWithScrollView:self.tableView playerManager:playerManager containerView:self.headerView.coverImageView];
  39. self.player.playerDisapperaPercent = 1.0;
  40. self.player.playerApperaPercent = 0.0;
  41. self.player.stopWhileNotVisible = NO;
  42. CGFloat margin = 20;
  43. CGFloat w = ZFPlayer_ScreenWidth/2;
  44. CGFloat h = w * 9/16;
  45. CGFloat x = ZFPlayer_ScreenWidth - w - margin;
  46. CGFloat y = ZFPlayer_ScreenHeight - h - margin;
  47. self.player.smallFloatView.frame = CGRectMake(x, y, w, h);
  48. self.player.controlView = self.controlView;
  49. @zf_weakify(self)
  50. self.player.playerDidToEnd = ^(id _Nonnull asset) {
  51. @zf_strongify(self)
  52. [self.player stopCurrentPlayingCell];
  53. };
  54. [self playTheIndex:0];
  55. }
  56. - (void)requestData {
  57. self.dataSource = @[].mutableCopy;
  58. NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
  59. NSData *data = [NSData dataWithContentsOfFile:path];
  60. NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
  61. NSArray *videoList = [rootDict objectForKey:@"list"];
  62. for (NSDictionary *dataDic in videoList) {
  63. ZFTableData *data = [[ZFTableData alloc] init];
  64. [data setValuesForKeysWithDictionary:dataDic];
  65. [self.dataSource addObject:data];
  66. }
  67. }
  68. - (void)viewWillLayoutSubviews {
  69. [super viewWillLayoutSubviews];
  70. CGFloat y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
  71. CGFloat h = CGRectGetMaxY(self.view.frame);
  72. self.tableView.frame = CGRectMake(0, y, self.view.frame.size.width, h-y);
  73. }
  74. - (BOOL)shouldAutorotate {
  75. return NO;
  76. }
  77. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  78. return UIInterfaceOrientationMaskPortrait;
  79. }
  80. - (UIStatusBarStyle)preferredStatusBarStyle {
  81. return UIStatusBarStyleDefault;
  82. }
  83. - (BOOL)prefersStatusBarHidden {
  84. return NO;
  85. }
  86. #pragma mark - UIScrollViewDelegate
  87. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  88. [scrollView zf_scrollViewDidScroll];
  89. }
  90. #pragma mark - UITableViewDataSource
  91. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  92. return self.dataSource.count;
  93. }
  94. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  95. ZFOtherCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentifier];
  96. cell.textLabel.text = [NSString stringWithFormat:@"点击播放第%zd个视频",indexPath.row + 1];
  97. return cell;
  98. }
  99. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  100. [self playTheIndex:indexPath.row];
  101. }
  102. #pragma mark - private
  103. - (void)playTheIndex:(NSInteger)index {
  104. /// 在这里判断能否播放。。。
  105. ZFTableData *data = self.dataSource[index];
  106. self.player.currentPlayerManager.assetURL = [NSURL URLWithString:data.video_url];
  107. [self.controlView showTitle:data.title coverURLString:data.thumbnail_url fullScreenMode:ZFFullScreenModeLandscape];
  108. if (self.tableView.contentOffset.y > self.headerView.frame.size.height) {
  109. [self.player addPlayerViewToSmallFloatView];
  110. } else {
  111. [self.player addPlayerViewToContainerView:self.headerView.coverImageView];
  112. }
  113. }
  114. #pragma mark - getter
  115. - (UITableView *)tableView {
  116. if (!_tableView) {
  117. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  118. [_tableView registerClass:[ZFOtherCell class] forCellReuseIdentifier:kIdentifier];
  119. _tableView.delegate = self;
  120. _tableView.dataSource = self;
  121. if (@available(iOS 11.0, *)) {
  122. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  123. } else {
  124. self.automaticallyAdjustsScrollViewInsets = NO;
  125. }
  126. _tableView.estimatedRowHeight = 0;
  127. _tableView.estimatedSectionFooterHeight = 0;
  128. _tableView.estimatedSectionHeaderHeight = 0;
  129. _tableView.rowHeight = 100;
  130. }
  131. return _tableView;
  132. }
  133. - (ZFPlayerControlView *)controlView {
  134. if (!_controlView) {
  135. _controlView = [ZFPlayerControlView new];
  136. _controlView.fastViewAnimated = YES;
  137. _controlView.prepareShowLoading = YES;
  138. }
  139. return _controlView;
  140. }
  141. - (ZFTableHeaderView *)headerView {
  142. if (!_headerView) {
  143. _headerView = [[ZFTableHeaderView alloc] init];
  144. @zf_weakify(self)
  145. _headerView.playCallback = ^{
  146. @zf_strongify(self)
  147. [self playTheIndex:0];
  148. };
  149. }
  150. return _headerView;
  151. }
  152. @end