// // audioPlayingView.m // Private-x // // Created by xd h on 2024/7/21. // #import "audioPlayingView.h" #import "DFPlayer.h" #import "audioPlayListManager.h" @interface audioPlayingView () @property(nonatomic,strong)UIImageView*bgImageView; @property (nonatomic, strong) CABasicAnimation *rotationAnimation; @property (nonatomic, strong)UIButton *playOrPausebut; @property (nonatomic, strong)UILabel *audioTitleLabel; @end @implementation audioPlayingView // 静态变量,用于存储单例实例 static audioPlayingView *sharedInstance = nil; + (instancetype)sharedInstance { // 使用dispatch_once保证线程安全 static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[self alloc] init]; }); return sharedInstance; } - (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; self.backgroundColor = [UIColor whiteColor]; [self drawAnyView]; return self; } -(void)drawAnyView { //上 线 UIView *toplineView = [[UIView alloc] init]; toplineView.backgroundColor = [UIColor hwColor:@"#E9E9E9"]; [self addSubview:toplineView]; [toplineView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.right.mas_equalTo(0); make.height.mas_equalTo(1); make.top.mas_equalTo(0); }]; //下 线 UIView *bottomlineView = [[UIView alloc] init]; bottomlineView.backgroundColor = [UIColor hwColor:@"#E9E9E9"]; [self addSubview:bottomlineView]; [bottomlineView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.right.mas_equalTo(0); make.height.mas_equalTo(1); make.bottom.mas_equalTo(-1); }]; //旋转图片 _bgImageView = [UIImageView new]; _bgImageView.image = [UIImage imageNamed:@"nas_audio_playing_img"]; [self addSubview:_bgImageView]; [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(0); make.width.mas_equalTo(40); make.height.mas_equalTo(40); make.left.mas_equalTo(16); }]; //删除按钮 UIButton *deletebut = [[UIButton alloc] init]; deletebut.tag = 1; [deletebut addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside]; [deletebut setImage:[UIImage imageNamed:@"nas_audio_playing_del_icon"] forState:UIControlStateNormal]; [self addSubview:deletebut]; [deletebut mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(-10); make.width.mas_equalTo(30); make.height.mas_equalTo(40); make.centerY.mas_equalTo(0); }]; //播放或者状态按钮 _playOrPausebut = [[UIButton alloc] init]; _playOrPausebut.tag = 2; [_playOrPausebut addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside]; [_playOrPausebut setImage:[UIImage imageNamed:@"nas_audio_playing_play_icon"] forState:UIControlStateSelected]; [_playOrPausebut setImage:[UIImage imageNamed:@"nas_audio_playing_pause_icon"] forState:UIControlStateNormal]; [self addSubview:_playOrPausebut]; [_playOrPausebut mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(deletebut.mas_left).offset(-10); make.width.mas_equalTo(30); make.height.mas_equalTo(40); make.centerY.mas_equalTo(0); }]; //音频标题 _audioTitleLabel = [[UILabel alloc] init]; _audioTitleLabel.font = [UIFont systemFontOfSize:12.0]; _audioTitleLabel.textColor = [UIColor hwColor:@"#666666"]; //_audioTitleLabel.text = @""; [self addSubview:_audioTitleLabel]; [_audioTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(20); make.left.equalTo(_bgImageView.mas_right).offset(8); make.right.equalTo(_playOrPausebut.mas_left).offset(-10); make.centerY.mas_equalTo(0); }]; //点击标题按钮 UIButton *titlebut = [[UIButton alloc] init]; titlebut.tag = 5; [titlebut addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:titlebut]; [titlebut mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(_audioTitleLabel.mas_left).offset(0); make.right.equalTo(_audioTitleLabel.mas_right).offset(0); make.height.mas_equalTo(40); make.centerY.mas_equalTo(0); }]; } #pragma mark 图片旋转相关 - (void)startRotatingImage { // 停止当前动画(如果有的话) [self stopRotatingImage]; // 创建一个CABasicAnimation实例 self.rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; // 设置动画的起始值(从0度开始) self.rotationAnimation.fromValue = [NSNumber numberWithFloat:0]; // 设置动画的结束值(旋转360度,注意这里是弧度制) self.rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2]; // 设置动画的持续时间 self.rotationAnimation.duration = 5.0; // 可以根据需要调整旋转速度 // 设置动画的重复次数,这里设置为HUGE_VALF表示无限次 self.rotationAnimation.repeatCount = HUGE_VALF; // 将动画添加到imageView的layer上 [self.bgImageView.layer addAnimation:self.rotationAnimation forKey:@"rotateAnimation"]; } - (void)stopRotatingImage { // 移除imageView上的动画 [self.bgImageView.layer removeAnimationForKey:@"rotateAnimation"]; [UIView animateWithDuration:5 animations:^{ self.bgImageView.layer.transform = CATransform3DIdentity; }]; } - (void)restartRotatingImage { // 直接调用startRotatingImage来重新启动动画 [self startRotatingImage]; } #pragma mark 按钮事件 - (void)didClickButtonFun:(UIButton*)but { NSInteger tag = but.tag; HLog(@"%ld",tag); if(tag == 1){ [[DFPlayer sharedPlayer] df_deallocPlayer]; [self removeFromSuperview]; } if(tag == 2){ if(but.selected){// [[DFPlayer sharedPlayer] df_play]; [self startRotatingImage]; } else{ [[DFPlayer sharedPlayer] df_pause]; [self stopRotatingImage]; } but.selected = !but.selected; } if(_didClickButtonFun){ _didClickButtonFun(tag); } } - (void)setAudioTitleFunBy:(NSInteger)AudioID { NSMutableArray*allDataArr = [audioPlayListManager shareManager].audioPlayListArr; if(allDataArr && allDataArr.count > AudioID){ lastFileModel *model = allDataArr[AudioID]; mainBlock(^{ self->_audioTitleLabel.text = model.name; }); } } - (void)setAudioPlayingStateFunBy:(BOOL)isPlaying { mainBlock(^{ if(isPlaying){ self->_playOrPausebut.selected = NO; [self startRotatingImage]; } else{ self->_playOrPausebut.selected = YES; [self stopRotatingImage]; } }); } @end