audioPlayingView.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // audioPlayingView.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/7/21.
  6. //
  7. #import "audioPlayingView.h"
  8. #import "DFPlayer.h"
  9. #import "audioPlayListManager.h"
  10. @interface audioPlayingView ()
  11. @property(nonatomic,strong)UIImageView*bgImageView;
  12. @property (nonatomic, strong) CABasicAnimation *rotationAnimation;
  13. @property (nonatomic, strong)UIButton *playOrPausebut;
  14. @property (nonatomic, strong)UILabel *audioTitleLabel;
  15. @end
  16. @implementation audioPlayingView
  17. // 静态变量,用于存储单例实例
  18. static audioPlayingView *sharedInstance = nil;
  19. + (instancetype)sharedInstance {
  20. // 使用dispatch_once保证线程安全
  21. static dispatch_once_t onceToken;
  22. dispatch_once(&onceToken, ^{
  23. sharedInstance = [[self alloc] init];
  24. });
  25. return sharedInstance;
  26. }
  27. - (id)initWithFrame:(CGRect)frame{
  28. self = [super initWithFrame:frame];
  29. self.backgroundColor = [UIColor whiteColor];
  30. [self drawAnyView];
  31. return self;
  32. }
  33. -(void)drawAnyView
  34. {
  35. //上 线
  36. UIView *toplineView = [[UIView alloc] init];
  37. toplineView.backgroundColor = [UIColor hwColor:@"#E9E9E9"];
  38. [self addSubview:toplineView];
  39. [toplineView mas_makeConstraints:^(MASConstraintMaker *make) {
  40. make.left.mas_equalTo(0);
  41. make.right.mas_equalTo(0);
  42. make.height.mas_equalTo(1);
  43. make.top.mas_equalTo(0);
  44. }];
  45. //下 线
  46. UIView *bottomlineView = [[UIView alloc] init];
  47. bottomlineView.backgroundColor = [UIColor hwColor:@"#E9E9E9"];
  48. [self addSubview:bottomlineView];
  49. [bottomlineView mas_makeConstraints:^(MASConstraintMaker *make) {
  50. make.left.mas_equalTo(0);
  51. make.right.mas_equalTo(0);
  52. make.height.mas_equalTo(1);
  53. make.bottom.mas_equalTo(-1);
  54. }];
  55. //旋转图片
  56. _bgImageView = [UIImageView new];
  57. _bgImageView.image = [UIImage imageNamed:@"nas_audio_playing_img"];
  58. [self addSubview:_bgImageView];
  59. [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  60. make.centerY.mas_equalTo(0);
  61. make.width.mas_equalTo(40);
  62. make.height.mas_equalTo(40);
  63. make.left.mas_equalTo(16);
  64. }];
  65. //删除按钮
  66. UIButton *deletebut = [[UIButton alloc] init];
  67. deletebut.tag = 1;
  68. [deletebut addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside];
  69. [deletebut setImage:[UIImage imageNamed:@"nas_audio_playing_del_icon"] forState:UIControlStateNormal];
  70. [self addSubview:deletebut];
  71. [deletebut mas_makeConstraints:^(MASConstraintMaker *make) {
  72. make.right.mas_equalTo(-10);
  73. make.width.mas_equalTo(30);
  74. make.height.mas_equalTo(40);
  75. make.centerY.mas_equalTo(0);
  76. }];
  77. //播放或者状态按钮
  78. _playOrPausebut = [[UIButton alloc] init];
  79. _playOrPausebut.tag = 2;
  80. [_playOrPausebut addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside];
  81. [_playOrPausebut setImage:[UIImage imageNamed:@"nas_audio_playing_play_icon"] forState:UIControlStateSelected];
  82. [_playOrPausebut setImage:[UIImage imageNamed:@"nas_audio_playing_pause_icon"] forState:UIControlStateNormal];
  83. [self addSubview:_playOrPausebut];
  84. [_playOrPausebut mas_makeConstraints:^(MASConstraintMaker *make) {
  85. make.right.equalTo(deletebut.mas_left).offset(-10);
  86. make.width.mas_equalTo(30);
  87. make.height.mas_equalTo(40);
  88. make.centerY.mas_equalTo(0);
  89. }];
  90. //音频标题
  91. _audioTitleLabel = [[UILabel alloc] init];
  92. _audioTitleLabel.font = [UIFont systemFontOfSize:12.0];
  93. _audioTitleLabel.textColor = [UIColor hwColor:@"#666666"];
  94. //_audioTitleLabel.text = @"";
  95. [self addSubview:_audioTitleLabel];
  96. [_audioTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  97. make.height.mas_equalTo(20);
  98. make.left.equalTo(_bgImageView.mas_right).offset(8);
  99. make.right.equalTo(_playOrPausebut.mas_left).offset(-10);
  100. make.centerY.mas_equalTo(0);
  101. }];
  102. //点击标题按钮
  103. UIButton *titlebut = [[UIButton alloc] init];
  104. titlebut.tag = 5;
  105. [titlebut addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside];
  106. [self addSubview:titlebut];
  107. [titlebut mas_makeConstraints:^(MASConstraintMaker *make) {
  108. make.left.equalTo(_audioTitleLabel.mas_left).offset(0);
  109. make.right.equalTo(_audioTitleLabel.mas_right).offset(0);
  110. make.height.mas_equalTo(40);
  111. make.centerY.mas_equalTo(0);
  112. }];
  113. }
  114. #pragma mark 图片旋转相关
  115. - (void)startRotatingImage {
  116. // 停止当前动画(如果有的话)
  117. [self stopRotatingImage];
  118. // 创建一个CABasicAnimation实例
  119. self.rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  120. // 设置动画的起始值(从0度开始)
  121. self.rotationAnimation.fromValue = [NSNumber numberWithFloat:0];
  122. // 设置动画的结束值(旋转360度,注意这里是弧度制)
  123. self.rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2];
  124. // 设置动画的持续时间
  125. self.rotationAnimation.duration = 5.0; // 可以根据需要调整旋转速度
  126. // 设置动画的重复次数,这里设置为HUGE_VALF表示无限次
  127. self.rotationAnimation.repeatCount = HUGE_VALF;
  128. // 将动画添加到imageView的layer上
  129. [self.bgImageView.layer addAnimation:self.rotationAnimation forKey:@"rotateAnimation"];
  130. }
  131. - (void)stopRotatingImage {
  132. // 移除imageView上的动画
  133. [self.bgImageView.layer removeAnimationForKey:@"rotateAnimation"];
  134. [UIView animateWithDuration:5 animations:^{
  135. self.bgImageView.layer.transform = CATransform3DIdentity;
  136. }];
  137. }
  138. - (void)restartRotatingImage {
  139. // 直接调用startRotatingImage来重新启动动画
  140. [self startRotatingImage];
  141. }
  142. #pragma mark 按钮事件
  143. - (void)didClickButtonFun:(UIButton*)but
  144. {
  145. NSInteger tag = but.tag;
  146. HLog(@"%ld",tag);
  147. if(tag == 1){
  148. [[DFPlayer sharedPlayer] df_deallocPlayer];
  149. [self removeFromSuperview];
  150. }
  151. if(tag == 2){
  152. if(but.selected){//
  153. [[DFPlayer sharedPlayer] df_play];
  154. [self startRotatingImage];
  155. }
  156. else{
  157. [[DFPlayer sharedPlayer] df_pause];
  158. [self stopRotatingImage];
  159. }
  160. but.selected = !but.selected;
  161. }
  162. if(_didClickButtonFun){
  163. _didClickButtonFun(tag);
  164. }
  165. }
  166. - (void)setAudioTitleFunBy:(NSInteger)AudioID
  167. {
  168. NSMutableArray*allDataArr = [audioPlayListManager shareManager].audioPlayListArr;
  169. if(allDataArr && allDataArr.count > AudioID){
  170. lastFileModel *model = allDataArr[AudioID];
  171. mainBlock(^{
  172. self->_audioTitleLabel.text = model.name;
  173. });
  174. }
  175. }
  176. - (void)setAudioPlayingStateFunBy:(BOOL)isPlaying
  177. {
  178. mainBlock(^{
  179. if(isPlaying){
  180. self->_playOrPausebut.selected = NO;
  181. [self startRotatingImage];
  182. }
  183. else{
  184. self->_playOrPausebut.selected = YES;
  185. [self stopRotatingImage];
  186. }
  187. });
  188. }
  189. @end