ZFDouyinCollectionViewCell.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // ZFDouyinCollectionViewCell.m
  3. // ZFPlayer_Example
  4. //
  5. // Created by 紫枫 on 2019/6/4.
  6. // Copyright © 2019 紫枫. All rights reserved.
  7. //
  8. #import "ZFDouyinCollectionViewCell.h"
  9. #import <ZFPlayer/UIImageView+ZFCache.h>
  10. #import <ZFPlayer/UIView+ZFFrame.h>
  11. #import <ZFPlayer/UIImageView+ZFCache.h>
  12. #import <ZFPlayer/ZFUtilities.h>
  13. #import "ZFLoadingView.h"
  14. @interface ZFDouyinCollectionViewCell ()
  15. @property (nonatomic, strong) UIImageView *coverImageView;
  16. @property (nonatomic, strong) UIButton *likeBtn;
  17. @property (nonatomic, strong) UIButton *commentBtn;
  18. @property (nonatomic, strong) UIButton *shareBtn;
  19. @property (nonatomic, strong) UILabel *titleLabel;
  20. @property (nonatomic, strong) UIImage *placeholderImage;
  21. @property (nonatomic, strong) UIButton *rotation;
  22. @end
  23. @implementation ZFDouyinCollectionViewCell
  24. - (instancetype)initWithFrame:(CGRect)frame {
  25. self = [super initWithFrame:frame];
  26. if (self) {
  27. self.contentView.backgroundColor = [UIColor blackColor];
  28. [self.contentView addSubview:self.coverImageView];
  29. [self.contentView addSubview:self.titleLabel];
  30. [self.contentView addSubview:self.likeBtn];
  31. [self.contentView addSubview:self.commentBtn];
  32. [self.contentView addSubview:self.shareBtn];
  33. [self.contentView addSubview:self.rotation];
  34. }
  35. return self;
  36. }
  37. - (void)layoutSubviews {
  38. [super layoutSubviews];
  39. self.coverImageView.frame = self.contentView.bounds;
  40. CGFloat min_x = 0;
  41. CGFloat min_y = 0;
  42. CGFloat min_w = 0;
  43. CGFloat min_h = 0;
  44. CGFloat min_view_w = self.zf_width;
  45. CGFloat min_view_h = self.zf_height;
  46. CGFloat margin = 30;
  47. min_w = 40;
  48. min_h = min_w;
  49. min_x = min_view_w - min_w - 20;
  50. min_y = min_view_h - min_h - 80;
  51. self.shareBtn.frame = CGRectMake(min_x, min_y, min_w, min_h);
  52. min_w = CGRectGetWidth(self.shareBtn.frame);
  53. min_h = min_w;
  54. min_x = CGRectGetMinX(self.shareBtn.frame);
  55. min_y = CGRectGetMinY(self.shareBtn.frame) - min_h - margin;
  56. self.commentBtn.frame = CGRectMake(min_x, min_y, min_w, min_h);
  57. min_w = CGRectGetWidth(self.shareBtn.frame);
  58. min_h = min_w;
  59. min_x = CGRectGetMinX(self.commentBtn.frame);
  60. min_y = CGRectGetMinY(self.commentBtn.frame) - min_h - margin;
  61. self.likeBtn.frame = CGRectMake(min_x, min_y, min_w, min_h);
  62. min_x = 20;
  63. min_h = 20;
  64. min_y = min_view_h - min_h - 50;
  65. min_w = self.likeBtn.zf_left - margin;
  66. self.titleLabel.frame = CGRectMake(min_x, min_y, min_w, min_h);
  67. min_x = 20;
  68. min_w = 50;
  69. min_h = 50;
  70. min_y = (min_view_h - min_h) / 2;
  71. self.rotation.frame = CGRectMake(min_x, min_y, min_w, min_h);
  72. }
  73. #pragma mark - action
  74. - (void)rotationClick {
  75. if ([self.delegate respondsToSelector:@selector(zf_douyinRotation)]) {
  76. [self.delegate zf_douyinRotation];
  77. }
  78. }
  79. #pragma mark - getter
  80. - (UILabel *)titleLabel {
  81. if (!_titleLabel) {
  82. _titleLabel = [UILabel new];
  83. _titleLabel.textColor = [UIColor whiteColor];
  84. _titleLabel.font = [UIFont systemFontOfSize:15];
  85. }
  86. return _titleLabel;
  87. }
  88. - (UIButton *)likeBtn {
  89. if (!_likeBtn) {
  90. _likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  91. [_likeBtn setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
  92. }
  93. return _likeBtn;
  94. }
  95. - (UIButton *)commentBtn {
  96. if (!_commentBtn) {
  97. _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  98. [_commentBtn setImage:[UIImage imageNamed:@"comment"] forState:UIControlStateNormal];
  99. }
  100. return _commentBtn;
  101. }
  102. - (UIButton *)shareBtn {
  103. if (!_shareBtn) {
  104. _shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  105. [_shareBtn setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
  106. }
  107. return _shareBtn;
  108. }
  109. - (UIImage *)placeholderImage {
  110. if (!_placeholderImage) {
  111. _placeholderImage = [ZFUtilities imageWithColor:[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1] size:CGSizeMake(1, 1)];
  112. }
  113. return _placeholderImage;
  114. }
  115. - (void)setData:(ZFTableData *)data {
  116. _data = data;
  117. [self.coverImageView setImageWithURLString:data.thumbnail_url placeholder:[UIImage imageNamed:@"loading_bgView"]];
  118. self.titleLabel.text = data.title;
  119. if (data.video_width > data.video_height) { /// 横屏视频才支持旋转
  120. self.rotation.hidden = NO;
  121. } else {
  122. self.rotation.hidden = YES;
  123. }
  124. }
  125. - (UIImageView *)coverImageView {
  126. if (!_coverImageView) {
  127. _coverImageView = [[UIImageView alloc] init];
  128. _coverImageView.userInteractionEnabled = YES;
  129. _coverImageView.tag = kPlayerViewTag;
  130. _coverImageView.contentMode = UIViewContentModeScaleAspectFit;
  131. }
  132. return _coverImageView;
  133. }
  134. - (UIButton *)rotation {
  135. if (!_rotation) {
  136. _rotation = [UIButton buttonWithType:UIButtonTypeCustom];
  137. [_rotation setImage:[UIImage imageNamed:@"zfplayer_rotaiton"] forState:UIControlStateNormal];
  138. [_rotation addTarget:self action:@selector(rotationClick) forControlEvents:UIControlEventTouchUpInside];
  139. }
  140. return _rotation;
  141. }
  142. @end