// // YOUPAIZYUserZiLiaoGRTopCell.m // MSYOUPAI // // Created by xiaohaoran on 2022/3/3. // Copyright © 2022 MS. All rights reserved. // #import "YOUPAIZYUserZiLiaoGRTopCell.h" #import "FSAudioStream.h" @interface YOUPAIZYUserZiLiaoGRTopCell() @property (nonatomic, weak) UIButton *youpaipAudioBtn; // 声音展示 @property (nonatomic, weak) UILabel *youpaipAudioL; // 声音时长 // 语音播放器 @property (nonatomic, strong)FSAudioStream *youpaipaudioStream; @property (nonatomic,weak) UIButton *youpaipaudioPlayBtn; @property (nonatomic, strong)NSTimer *youpaiptimer; @property (nonatomic, strong)UIView *youpaipVoiceView;//声音bg @property (nonatomic, strong)UIView *youpaipSignView;//签名bg @property (nonatomic, strong)UIView *youpaipBgView; @property (nonatomic, strong)UILabel *youpaipSignContentLabel;//签名 @end @implementation YOUPAIZYUserZiLiaoGRTopCell -(instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self youpaifsetSubView]; } return self; } -(void)youpaifsetSubView{ UIView *titleView = [UIView new]; [self.contentView addSubview:titleView]; [titleView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.right.mas_equalTo(0); make.top.mas_equalTo(0); make.height.mas_equalTo(17); }]; titleView.backgroundColor = [UIColor whiteColor]; self.youpaiptitleLabel = [[UILabel alloc] init]; [titleView addSubview:self.youpaiptitleLabel]; [self.youpaiptitleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(0); make.left.mas_equalTo(12); }]; self.youpaiptitleLabel.textColor = LZ273145Color; self.youpaiptitleLabel.font = LCBoldFont(14); //bg UIView *youpaipBgView = [UIView new]; self.youpaipBgView = youpaipBgView; [self.contentView addSubview:youpaipBgView]; [youpaipBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(12); make.right.mas_equalTo(-12); make.top.mas_equalTo(titleView.mas_bottom).offset(9); make.height.mas_equalTo(102); }]; youpaipBgView.backgroundColor = LZFAFAFCColor; //声音展示 UIView *youpaipVoiceView = [UIView new]; self.youpaipVoiceView = youpaipVoiceView; [youpaipBgView addSubview:youpaipVoiceView]; [youpaipVoiceView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.right.mas_equalTo(0); make.top.mas_equalTo(0); make.height.mas_equalTo(51); }]; youpaipVoiceView.backgroundColor = LZFAFAFCColor; //title UILabel *youpaipVoiceTitleLabel = [UILabel new]; [youpaipVoiceView addSubview:youpaipVoiceTitleLabel]; [youpaipVoiceTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(8); make.top.mas_equalTo(8); }]; youpaipVoiceTitleLabel.text = @"声音展示"; youpaipVoiceTitleLabel.textColor = LZA3AABEColor; youpaipVoiceTitleLabel.font = [UIFont systemFontOfSize:10]; // UIButton *youpaipAudioBtn = [UIButton buttonWithType:UIButtonTypeCustom]; self.youpaipaudioPlayBtn = youpaipAudioBtn; [youpaipAudioBtn setBackgroundImage:[UIImage imageNamed:@"vqu_images_voice_play"] forState:UIControlStateNormal]; [youpaipAudioBtn setBackgroundImage:[UIImage imageNamed:@"vqu_images_voice_stop"] forState:UIControlStateSelected]; [youpaipAudioBtn addTarget:self action:@selector(youpaipAudioBtnClick) forControlEvents:UIControlEventTouchUpInside]; [youpaipVoiceView addSubview:youpaipAudioBtn]; self.youpaipAudioBtn = youpaipAudioBtn; [youpaipAudioBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(7.5); make.top.mas_equalTo(youpaipVoiceTitleLabel.mas_bottom).offset(6); make.size.mas_equalTo(CGSizeMake(96, 25)); }]; UILabel *youpaipAudioL = [[UILabel alloc] init]; youpaipAudioL.font = LCFont(12.0f); youpaipAudioL.textColor = LCWhiteColor; [youpaipAudioBtn addSubview:youpaipAudioL]; self.youpaipAudioL = youpaipAudioL; [youpaipAudioL mas_makeConstraints:^(MASConstraintMaker *make) { make.right.offset(-12.0f); make.centerY.equalTo(youpaipAudioBtn); }]; // UIView *youpaipSignView = [UIView new]; self.youpaipSignView =youpaipSignView; [youpaipBgView addSubview:youpaipSignView]; [youpaipSignView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.right.mas_equalTo(0); make.top.mas_equalTo(youpaipVoiceView.mas_bottom); make.height.mas_equalTo(41); }]; youpaipSignView.backgroundColor = LZFAFAFCColor; //title UILabel *youpaipSignTitleLabel = [UILabel new]; [youpaipSignView addSubview:youpaipSignTitleLabel]; [youpaipSignTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(youpaipVoiceTitleLabel); make.top.mas_equalTo(10); }]; youpaipSignTitleLabel.text = @"个性签名"; youpaipSignTitleLabel.textColor = LZA3AABEColor; youpaipSignTitleLabel.font = [UIFont systemFontOfSize:10]; //签名内容 UILabel *youpaipSignContentLabel = [UILabel new]; self.youpaipSignContentLabel = youpaipSignContentLabel; [youpaipSignView addSubview:youpaipSignContentLabel]; [youpaipSignContentLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(youpaipSignTitleLabel); make.right.mas_equalTo(-8); make.top.mas_equalTo(youpaipSignTitleLabel.mas_bottom).offset(6); }]; // youpaipSignContentLabel.text = @"热爱生活、旅行、交友,欢迎来撩👏~备份"; youpaipSignContentLabel.textColor = LZ273145Color; youpaipSignContentLabel.font = [UIFont systemFontOfSize:12]; youpaipSignContentLabel.numberOfLines = 0; } -(void)setYoupaipmodel:(YOUPAILCUserShowModel *)youpaipmodel{ _youpaipmodel = youpaipmodel; self.youpaipAudioL.text = [NSString stringWithFormat:@"%@\"",@(youpaipmodel.youpaipvoice.youpaipvoice_time)]; CGFloat voiceHeight = 0; if (self.youpaipmodel.youpaipvoice.youpaipvoice_status == 1){ voiceHeight = 51; }else{ self.youpaipVoiceView.hidden = YES; [self.youpaipSignView mas_updateConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(0); }]; } NSString *content = self.youpaipmodel.youpaipsign; NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:content]; NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init]; paragraphStyle1.alignment=NSTextAlignmentJustified; NSDictionary * dic =@{ NSParagraphStyleAttributeName:paragraphStyle1, NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone], }; [attributedString1 setAttributes:dic range:NSMakeRange(0, attributedString1.length)]; [self.youpaipSignContentLabel setAttributedText:attributedString1]; CGFloat height = [content boundingRectWithSize:CGSizeMake(KScreenWidth - 40.0f, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName : LCFont12 } context:nil].size.height; [self.youpaipSignView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(height+6+12+10); }]; [self.youpaipBgView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(voiceHeight+height+6+12+20); }]; self.youpaipSignView.layer.cornerRadius = 8; self.youpaipSignView.layer.masksToBounds = YES; self.youpaipBgView.layer.cornerRadius = 8; self.youpaipBgView.layer.masksToBounds = YES; } - (void)youpaifinitAudioStream{ self.youpaipaudioStream = [[FSAudioStream alloc] init]; @weakify(self); self.youpaipaudioStream.onFailure = ^(FSAudioStreamError error,NSString *description){ @strongify(self); [self youpaifstopPlay]; }; self.youpaipaudioStream.onCompletion = ^(){ @strongify(self); [self youpaifstopPlay]; }; [self.youpaipaudioStream setVolume:0.7];//设置声音 self.youpaipaudioStream.strictContentTypeChecking = NO; self.youpaipaudioStream.defaultContentType = @"audio/mpeg"; } /// 播放or暂停 - (void)youpaipAudioBtnClick{ self.youpaipaudioPlayBtn.selected = !self.youpaipaudioPlayBtn.selected; if (self.youpaipaudioPlayBtn.selected) { [self youpaifstartPlay]; [self youpaifstartTimer]; }else{ [self youpaifstopPlay]; } } /// 开始播放语音 - (void)youpaifstartPlay{ if (self.youpaipaudioStream == nil) { [self youpaifinitAudioStream]; } NSURL *url = [LCTools getImageUrlWithAddress:self.youpaipmodel.youpaipvoice.youpaipvoice]; self.youpaipaudioStream.url = url; [self.youpaipaudioStream play]; } /// 停止播放语音 - (void)youpaifstopPlay{ self.youpaipaudioPlayBtn.selected = NO; self.youpaipAudioL.text = [NSString stringWithFormat:@"%@\"",@(self.youpaipmodel.youpaipvoice.youpaipvoice_time)]; // 停止播放 if (self.youpaipaudioStream != nil || [self.youpaipaudioStream isPlaying]) { [self.youpaipaudioStream stop]; self.youpaipaudioStream = nil; } [self youpaifstopTimer]; } // 开始计时 - (void)youpaifstartTimer{ self.youpaiptimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(youpaiftimerAction) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:self.youpaiptimer forMode:NSRunLoopCommonModes]; } // 结束计时 - (void)youpaifstopTimer{ [self.youpaiptimer invalidate]; self.youpaiptimer = nil; } - (void)youpaiftimerAction{ NSInteger timer = self.youpaipmodel.youpaipvoice.youpaipvoice_time - self.youpaipaudioStream.currentTimePlayed.playbackTimeInSeconds; self.youpaipAudioL.text = [NSString stringWithFormat:@"%@\"",@(timer)]; } - (void)didMoveToWindow{ [super didMoveToWindow]; [self youpaifstopPlay]; } @end