RITLPhotosBrowseLiveCell.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // RITLPhotosBrowseLiveCell.m
  3. // RITLPhotoDemo
  4. //
  5. // Created by YueWen on 2018/5/9.
  6. // Copyright © 2018年 YueWen. All rights reserved.
  7. //
  8. #import "RITLPhotosBrowseLiveCell.h"
  9. #import "UICollectionViewCell+RITLPhotosAsset.h"
  10. #import "RITLKit.h"
  11. #import <PhotosUI/PhotosUI.h>
  12. #import "Masonry.h"
  13. @interface RITLPhotosBrowseLiveCell() <PHLivePhotoViewDelegate>
  14. /// 是否是按压唤醒
  15. @property (nonatomic, assign)BOOL isForce;
  16. @property (nonatomic, strong)UITapGestureRecognizer *tapGestureRecognizer;
  17. @end
  18. @implementation RITLPhotosBrowseLiveCell
  19. - (instancetype)initWithFrame:(CGRect)frame
  20. {
  21. if (self = [super initWithFrame:frame]) {
  22. [self buildViews];
  23. }
  24. return self;
  25. }
  26. - (void)prepareForReuse
  27. {
  28. [self stop];
  29. }
  30. - (void)buildViews
  31. {
  32. self.isPlaying = false;
  33. self.imageView = [[UIImageView alloc]init];
  34. self.imageView.contentMode = UIViewContentModeScaleAspectFit;
  35. [self.contentView addSubview:self.imageView];
  36. [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.edges.offset(0);
  38. }];
  39. self.liveBadgeImageView = ({
  40. UIImageView *imageView = [UIImageView new];
  41. imageView.backgroundColor = UIColor.clearColor;
  42. imageView.contentMode = UIViewContentModeScaleToFill;
  43. imageView.image = [PHLivePhotoView livePhotoBadgeImageWithOptions:PHLivePhotoBadgeOptionsOverContent];
  44. imageView;
  45. });
  46. [self.contentView addSubview:self.liveBadgeImageView];
  47. [self.liveBadgeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  48. make.height.width.mas_equalTo(25);
  49. make.left.offset(10);
  50. make.top.offset(RITL_DefaultNaviBarHeight + 18);
  51. }];
  52. self.liveLabel = ({
  53. UILabel *label = [UILabel new];
  54. label.text = @"Live";
  55. label.font = RITLUtilityFont(RITLFontPingFangSC_Regular, 14);
  56. label.textColor = UIColor.whiteColor;
  57. label;
  58. });
  59. [self.contentView addSubview:self.liveLabel];
  60. [self.liveLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  61. make.centerY.equalTo(self.liveBadgeImageView);
  62. make.left.equalTo(self.liveBadgeImageView.mas_right).offset(3);
  63. }];
  64. self.livePhotoView = ({
  65. PHLivePhotoView *view = [PHLivePhotoView new];
  66. view.hidden = true;
  67. view.delegate = self;
  68. view.userInteractionEnabled = false;
  69. view;
  70. });
  71. [self.contentView addSubview:self.livePhotoView];
  72. [self.livePhotoView mas_makeConstraints:^(MASConstraintMaker *make) {
  73. make.center.equalTo(self.imageView);
  74. }];
  75. __weak typeof(self) weak = self;
  76. //追加点击时间
  77. self.tapGestureRecognizer = [self.contentView addTapGestureRecognizerNumberOfTap:1 Handler:^(UIView * _Nonnull view) {
  78. if (weak.isPlaying) { [weak stop]; return; }
  79. else if (!weak.isPlaying && weak.livePhotoView.hidden) {//处理0.2秒的延迟
  80. [weak playerAsset];
  81. }
  82. }];
  83. }
  84. #pragma mark - <PHLivePhotoViewDelegate>
  85. - (void)livePhotoView:(PHLivePhotoView *)livePhotoView willBeginPlaybackWithStyle:(PHLivePhotoViewPlaybackStyle)playbackStyle
  86. {
  87. self.isPlaying = true;
  88. self.livePhotoView.hidden = false;
  89. }
  90. - (void)livePhotoView:(PHLivePhotoView *)livePhotoView didEndPlaybackWithStyle:(PHLivePhotoViewPlaybackStyle)playbackStyle
  91. {
  92. self.isPlaying = false;
  93. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  94. self.livePhotoView.hidden = true;
  95. });
  96. }
  97. @end