YOUPAIZYUserZiLiaoGRTopCell.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //
  2. // YOUPAIZYUserZiLiaoGRTopCell.m
  3. // MSYOUPAI
  4. //
  5. // Created by xiaohaoran on 2022/3/3.
  6. // Copyright © 2022 MS. All rights reserved.
  7. //
  8. #import "YOUPAIZYUserZiLiaoGRTopCell.h"
  9. #import "FSAudioStream.h"
  10. @interface YOUPAIZYUserZiLiaoGRTopCell()
  11. @property (nonatomic, weak) UIButton *youpaipAudioBtn; // 声音展示
  12. @property (nonatomic, weak) UILabel *youpaipAudioL; // 声音时长
  13. // 语音播放器
  14. @property (nonatomic, strong)FSAudioStream *youpaipaudioStream;
  15. @property (nonatomic,weak) UIButton *youpaipaudioPlayBtn;
  16. @property (nonatomic, strong)NSTimer *youpaiptimer;
  17. @property (nonatomic, strong)UIView *youpaipVoiceView;//声音bg
  18. @property (nonatomic, strong)UIView *youpaipSignView;//签名bg
  19. @property (nonatomic, strong)UIView *youpaipBgView;
  20. @property (nonatomic, strong)UILabel *youpaipSignContentLabel;//签名
  21. @end
  22. @implementation YOUPAIZYUserZiLiaoGRTopCell
  23. -(instancetype)initWithFrame:(CGRect)frame{
  24. if (self = [super initWithFrame:frame]) {
  25. [self youpaifsetSubView];
  26. }
  27. return self;
  28. }
  29. -(void)youpaifsetSubView{
  30. UIView *titleView = [UIView new];
  31. [self.contentView addSubview:titleView];
  32. [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
  33. make.left.mas_equalTo(0);
  34. make.right.mas_equalTo(0);
  35. make.top.mas_equalTo(0);
  36. make.height.mas_equalTo(17);
  37. }];
  38. titleView.backgroundColor = [UIColor whiteColor];
  39. self.youpaiptitleLabel = [[UILabel alloc] init];
  40. [titleView addSubview:self.youpaiptitleLabel];
  41. [self.youpaiptitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  42. make.centerY.mas_equalTo(0);
  43. make.left.mas_equalTo(12);
  44. }];
  45. self.youpaiptitleLabel.textColor = LZ273145Color;
  46. self.youpaiptitleLabel.font = LCBoldFont(14);
  47. //bg
  48. UIView *youpaipBgView = [UIView new];
  49. self.youpaipBgView = youpaipBgView;
  50. [self.contentView addSubview:youpaipBgView];
  51. [youpaipBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  52. make.left.mas_equalTo(12);
  53. make.right.mas_equalTo(-12);
  54. make.top.mas_equalTo(titleView.mas_bottom).offset(9);
  55. make.height.mas_equalTo(102);
  56. }];
  57. youpaipBgView.backgroundColor = LZFAFAFCColor;
  58. //声音展示
  59. UIView *youpaipVoiceView = [UIView new];
  60. self.youpaipVoiceView = youpaipVoiceView;
  61. [youpaipBgView addSubview:youpaipVoiceView];
  62. [youpaipVoiceView mas_makeConstraints:^(MASConstraintMaker *make) {
  63. make.left.mas_equalTo(0);
  64. make.right.mas_equalTo(0);
  65. make.top.mas_equalTo(0);
  66. make.height.mas_equalTo(51);
  67. }];
  68. youpaipVoiceView.backgroundColor = LZFAFAFCColor;
  69. //title
  70. UILabel *youpaipVoiceTitleLabel = [UILabel new];
  71. [youpaipVoiceView addSubview:youpaipVoiceTitleLabel];
  72. [youpaipVoiceTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  73. make.left.mas_equalTo(8);
  74. make.top.mas_equalTo(8);
  75. }];
  76. youpaipVoiceTitleLabel.text = @"声音展示";
  77. youpaipVoiceTitleLabel.textColor = LZA3AABEColor;
  78. youpaipVoiceTitleLabel.font = [UIFont systemFontOfSize:10];
  79. //
  80. UIButton *youpaipAudioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  81. self.youpaipaudioPlayBtn = youpaipAudioBtn;
  82. [youpaipAudioBtn setBackgroundImage:[UIImage imageNamed:@"vqu_images_voice_play"] forState:UIControlStateNormal];
  83. [youpaipAudioBtn setBackgroundImage:[UIImage imageNamed:@"vqu_images_voice_stop"] forState:UIControlStateSelected];
  84. [youpaipAudioBtn addTarget:self action:@selector(youpaipAudioBtnClick) forControlEvents:UIControlEventTouchUpInside];
  85. [youpaipVoiceView addSubview:youpaipAudioBtn];
  86. self.youpaipAudioBtn = youpaipAudioBtn;
  87. [youpaipAudioBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  88. make.left.mas_equalTo(7.5);
  89. make.top.mas_equalTo(youpaipVoiceTitleLabel.mas_bottom).offset(6);
  90. make.size.mas_equalTo(CGSizeMake(96, 25));
  91. }];
  92. UILabel *youpaipAudioL = [[UILabel alloc] init];
  93. youpaipAudioL.font = LCFont(12.0f);
  94. youpaipAudioL.textColor = LCWhiteColor;
  95. [youpaipAudioBtn addSubview:youpaipAudioL];
  96. self.youpaipAudioL = youpaipAudioL;
  97. [youpaipAudioL mas_makeConstraints:^(MASConstraintMaker *make) {
  98. make.right.offset(-12.0f);
  99. make.centerY.equalTo(youpaipAudioBtn);
  100. }];
  101. //
  102. UIView *youpaipSignView = [UIView new];
  103. self.youpaipSignView =youpaipSignView;
  104. [youpaipBgView addSubview:youpaipSignView];
  105. [youpaipSignView mas_makeConstraints:^(MASConstraintMaker *make) {
  106. make.left.mas_equalTo(0);
  107. make.right.mas_equalTo(0);
  108. make.top.mas_equalTo(youpaipVoiceView.mas_bottom);
  109. make.height.mas_equalTo(41);
  110. }];
  111. youpaipSignView.backgroundColor = LZFAFAFCColor;
  112. //title
  113. UILabel *youpaipSignTitleLabel = [UILabel new];
  114. [youpaipSignView addSubview:youpaipSignTitleLabel];
  115. [youpaipSignTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  116. make.left.mas_equalTo(youpaipVoiceTitleLabel);
  117. make.top.mas_equalTo(10);
  118. }];
  119. youpaipSignTitleLabel.text = @"个性签名";
  120. youpaipSignTitleLabel.textColor = LZA3AABEColor;
  121. youpaipSignTitleLabel.font = [UIFont systemFontOfSize:10];
  122. //签名内容
  123. UILabel *youpaipSignContentLabel = [UILabel new];
  124. self.youpaipSignContentLabel = youpaipSignContentLabel;
  125. [youpaipSignView addSubview:youpaipSignContentLabel];
  126. [youpaipSignContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  127. make.left.mas_equalTo(youpaipSignTitleLabel);
  128. make.right.mas_equalTo(-8);
  129. make.top.mas_equalTo(youpaipSignTitleLabel.mas_bottom).offset(6);
  130. }];
  131. // youpaipSignContentLabel.text = @"热爱生活、旅行、交友,欢迎来撩👏~备份";
  132. youpaipSignContentLabel.textColor = LZ273145Color;
  133. youpaipSignContentLabel.font = [UIFont systemFontOfSize:12];
  134. youpaipSignContentLabel.numberOfLines = 0;
  135. }
  136. -(void)setYoupaipmodel:(YOUPAILCUserShowModel *)youpaipmodel{
  137. _youpaipmodel = youpaipmodel;
  138. self.youpaipAudioL.text = [NSString stringWithFormat:@"%@\"",@(youpaipmodel.youpaipvoice.youpaipvoice_time)];
  139. CGFloat voiceHeight = 0;
  140. if (self.youpaipmodel.youpaipvoice.youpaipvoice_status == 1){
  141. voiceHeight = 51;
  142. }else{
  143. self.youpaipVoiceView.hidden = YES;
  144. [self.youpaipSignView mas_updateConstraints:^(MASConstraintMaker *make) {
  145. make.top.mas_equalTo(0);
  146. }];
  147. }
  148. NSString *content = self.youpaipmodel.youpaipsign;
  149. NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:content];
  150. NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
  151. paragraphStyle1.alignment=NSTextAlignmentJustified;
  152. NSDictionary * dic =@{
  153. NSParagraphStyleAttributeName:paragraphStyle1,
  154. NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone],
  155. };
  156. [attributedString1 setAttributes:dic range:NSMakeRange(0, attributedString1.length)];
  157. [self.youpaipSignContentLabel setAttributedText:attributedString1];
  158. CGFloat height = [content boundingRectWithSize:CGSizeMake(KScreenWidth - 40.0f, MAXFLOAT)
  159. options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
  160. attributes:@{ NSFontAttributeName : LCFont12 } context:nil].size.height;
  161. [self.youpaipSignView mas_updateConstraints:^(MASConstraintMaker *make) {
  162. make.height.mas_equalTo(height+6+12+10);
  163. }];
  164. [self.youpaipBgView mas_updateConstraints:^(MASConstraintMaker *make) {
  165. make.height.mas_equalTo(voiceHeight+height+6+12+20);
  166. }];
  167. self.youpaipSignView.layer.cornerRadius = 8;
  168. self.youpaipSignView.layer.masksToBounds = YES;
  169. self.youpaipBgView.layer.cornerRadius = 8;
  170. self.youpaipBgView.layer.masksToBounds = YES;
  171. }
  172. - (void)youpaifinitAudioStream{
  173. self.youpaipaudioStream = [[FSAudioStream alloc] init];
  174. @weakify(self);
  175. self.youpaipaudioStream.onFailure = ^(FSAudioStreamError error,NSString *description){
  176. @strongify(self);
  177. [self youpaifstopPlay];
  178. };
  179. self.youpaipaudioStream.onCompletion = ^(){
  180. @strongify(self);
  181. [self youpaifstopPlay];
  182. };
  183. [self.youpaipaudioStream setVolume:0.7];//设置声音
  184. self.youpaipaudioStream.strictContentTypeChecking = NO;
  185. self.youpaipaudioStream.defaultContentType = @"audio/mpeg";
  186. }
  187. /// 播放or暂停
  188. - (void)youpaipAudioBtnClick{
  189. self.youpaipaudioPlayBtn.selected = !self.youpaipaudioPlayBtn.selected;
  190. if (self.youpaipaudioPlayBtn.selected) {
  191. [self youpaifstartPlay];
  192. [self youpaifstartTimer];
  193. }else{
  194. [self youpaifstopPlay];
  195. }
  196. }
  197. /// 开始播放语音
  198. - (void)youpaifstartPlay{
  199. if (self.youpaipaudioStream == nil) {
  200. [self youpaifinitAudioStream];
  201. }
  202. NSURL *url = [LCTools getImageUrlWithAddress:self.youpaipmodel.youpaipvoice.youpaipvoice];
  203. self.youpaipaudioStream.url = url;
  204. [self.youpaipaudioStream play];
  205. }
  206. /// 停止播放语音
  207. - (void)youpaifstopPlay{
  208. self.youpaipaudioPlayBtn.selected = NO;
  209. self.youpaipAudioL.text = [NSString stringWithFormat:@"%@\"",@(self.youpaipmodel.youpaipvoice.youpaipvoice_time)];
  210. // 停止播放
  211. if (self.youpaipaudioStream != nil || [self.youpaipaudioStream isPlaying]) {
  212. [self.youpaipaudioStream stop];
  213. self.youpaipaudioStream = nil;
  214. }
  215. [self youpaifstopTimer];
  216. }
  217. // 开始计时
  218. - (void)youpaifstartTimer{
  219. self.youpaiptimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(youpaiftimerAction) userInfo:nil repeats:YES];
  220. [[NSRunLoop currentRunLoop] addTimer:self.youpaiptimer forMode:NSRunLoopCommonModes];
  221. }
  222. // 结束计时
  223. - (void)youpaifstopTimer{
  224. [self.youpaiptimer invalidate];
  225. self.youpaiptimer = nil;
  226. }
  227. - (void)youpaiftimerAction{
  228. NSInteger timer = self.youpaipmodel.youpaipvoice.youpaipvoice_time - self.youpaipaudioStream.currentTimePlayed.playbackTimeInSeconds;
  229. self.youpaipAudioL.text = [NSString stringWithFormat:@"%@\"",@(timer)];
  230. }
  231. - (void)didMoveToWindow{
  232. [super didMoveToWindow];
  233. [self youpaifstopPlay];
  234. }
  235. @end