PQStuckPointMusicContentCell.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // PQStuckPointMusicContentCell.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2021/4/28.
  6. // Copyright © 2021 BytesFlow. All rights reserved.
  7. //
  8. import UIKit
  9. import BFCommonKit
  10. class PQStuckPointMusicContentCell: UICollectionViewCell {
  11. // 按钮点击的回调
  12. var btnClickHandle: ((_ sender: UIButton, _ bgmData: Any?) -> Void)?
  13. var contentType: stuckPointMusicContentType = .catagery
  14. lazy var audioImageView: UIImageView = {
  15. let audioImageView = UIImageView(image:bfFramworkImage(by: "videomk_music_default")!)
  16. audioImageView.addCorner(corner: 4)
  17. audioImageView.contentMode = .scaleAspectFill
  18. return audioImageView
  19. }()
  20. lazy var imageMaskView: UIView = {
  21. let imageMaskView = UIView()
  22. imageMaskView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
  23. imageMaskView.addCorner(corner: 4)
  24. return imageMaskView
  25. }()
  26. lazy var playImageView: UIImageView = {
  27. let playImageView = UIImageView()
  28. playImageView.image = UIImage.moduleImage(named: "stuckPoint_music_pause", moduleName: "BFFramework",isAssets: false)
  29. return playImageView
  30. }()
  31. lazy var titleLab: UILabel = {
  32. let titleLab = UILabel()
  33. titleLab.font = UIFont.systemFont(ofSize: 14)
  34. titleLab.textColor = BFConfig.shared.styleTitleColor
  35. return titleLab
  36. }()
  37. /// 音乐歌曲名称
  38. lazy var musicNameLab: LMJHorizontalScrollText = {
  39. let musicNameLab = LMJHorizontalScrollText(frame: CGRect(x: 0, y: 0, width: cDefaultMargin * 6, height: cDefaultMargin * 3))
  40. musicNameLab.textColor = BFConfig.shared.styleTitleColor
  41. musicNameLab.textFont = UIFont.systemFont(ofSize: 16)
  42. musicNameLab.speed = 0.03
  43. musicNameLab.moveDirection = LMJTextScrollMoveLeft
  44. musicNameLab.moveMode = LMJTextScrollContinuous
  45. musicNameLab.stop()
  46. return musicNameLab
  47. }()
  48. // 使用按钮
  49. lazy var confirmContentView: UIView = {
  50. let confirmContentView = UIView()
  51. confirmContentView.backgroundColor = BFConfig.shared.styleBackGroundColor
  52. let ges = UITapGestureRecognizer(target: self, action: #selector(confirmClick))
  53. confirmContentView.addGestureRecognizer(ges)
  54. confirmContentView.isHidden = true
  55. return confirmContentView
  56. }()
  57. // 使用按钮
  58. lazy var confirmBtn: UIButton = {
  59. let confirmBtn = UIButton(type: .custom)
  60. confirmBtn.setTitle(" 使用 ", for: .normal)
  61. confirmBtn.setTitleColor(UIColor.white, for: .normal)
  62. confirmBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
  63. confirmBtn.isUserInteractionEnabled = false
  64. confirmBtn.addCorner(corner: 5)
  65. confirmBtn.backgroundColor = UIColor.hexColor(hexadecimal: BFConfig.shared.styleColor.rawValue)
  66. return confirmBtn
  67. }()
  68. lazy var remindView: UIView = {
  69. let remindView = UIView()
  70. remindView.backgroundColor = UIColor.hexColor(hexadecimal: BFConfig.shared.styleColor.rawValue)
  71. remindView.addCorner(corner: 3)
  72. return remindView
  73. }()
  74. @objc class func stuckPointMusicContentCell(collectionView: UICollectionView, indexPath: IndexPath) -> PQStuckPointMusicContentCell {
  75. let cell: PQStuckPointMusicContentCell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: PQStuckPointMusicContentCell.self), for: indexPath) as! PQStuckPointMusicContentCell
  76. return cell
  77. }
  78. override init(frame: CGRect) {
  79. super.init(frame: frame)
  80. contentView.addSubview(audioImageView)
  81. audioImageView.addSubview(imageMaskView)
  82. audioImageView.addSubview(playImageView)
  83. contentView.addSubview(titleLab)
  84. contentView.addSubview(musicNameLab)
  85. contentView.addSubview(remindView)
  86. contentView.addSubview(confirmContentView)
  87. confirmContentView.addSubview(confirmBtn)
  88. PQNotification.addObserver(forName: Notification.Name(rawValue: "MusicContentCellIconLoadingAnimationStop"), object: nil, queue: .main) { [weak self] notice in
  89. if !(self?.imageMaskView.isHidden ?? true){
  90. self?.stopLoadingAnimation()
  91. }
  92. }
  93. }
  94. required init?(coder _: NSCoder) {
  95. fatalError("init(coder:) has not been implemented")
  96. }
  97. deinit {
  98. PQNotification.removeObserver(self)
  99. }
  100. var bgmData: Any? {
  101. didSet {
  102. addData()
  103. addLayout()
  104. }
  105. }
  106. func addData() {
  107. imageMaskView.isHidden = !(bgmData is PQVoiceModel) || BFConfig.shared.hiddenMusicMask
  108. musicNameLab.isHidden = !(bgmData is PQVoiceModel)
  109. titleLab.isHidden = (bgmData is PQVoiceModel)
  110. if bgmData is PQVoiceModel {
  111. audioImageView.setNetImage(url: "\((bgmData as? PQVoiceModel)?.avatarUrl ?? "")", placeholder: bfFramworkImage(by: "videomk_music_default")!)
  112. confirmContentView.isHidden = !((bgmData as? PQVoiceModel)?.isSelected ?? false)
  113. if (bgmData as? PQVoiceModel)?.isSelected ?? false {
  114. imageMaskView.isHidden = false
  115. playImageView.isHidden = false
  116. if (bgmData as? PQVoiceModel)?.isPlaying ?? false {
  117. if playImageView.image == nil {
  118. playImageView.image = UIImage.moduleImage(named: "loading", moduleName: "BFFramework", isAssets: false)
  119. startLoadingAnimation()
  120. }else {
  121. playImageView.kf.setImage(with: URL(fileURLWithPath: (currentBundlePath()!.path(forResource: "stuckPoint_music_playing", ofType: ".gif")!)))
  122. }
  123. musicNameLab.move()
  124. } else {
  125. playImageView.image = UIImage.moduleImage(named: "stuckPoint_music_pause", moduleName: "BFFramework",isAssets: false)
  126. musicNameLab.stop()
  127. }
  128. } else {
  129. playImageView.isHidden = true
  130. playImageView.image = nil
  131. musicNameLab.stop()
  132. }
  133. } else {
  134. if (bgmData as? PQStuckPointMusicTagsModel)?.tagEmoji != nil {
  135. audioImageView.setNetImage(url: "\((bgmData as? PQStuckPointMusicTagsModel)?.tagEmoji ?? "")", placeholder:bfFramworkImage(by: "videomk_music_default")!)
  136. } else {
  137. audioImageView.image = bfFramworkImage(by: "videomk_music_default")
  138. }
  139. titleLab.text = " \((bgmData as? PQStuckPointMusicTagsModel)?.tagName ?? "")"
  140. if (titleLab.text?.count ?? 0) > 8 {
  141. titleLab.font = UIFont.systemFont(ofSize: 8)
  142. } else if (titleLab.text?.count ?? 0) > 7 {
  143. titleLab.font = UIFont.systemFont(ofSize: 10)
  144. } else if (titleLab.text?.count ?? 0) > 6 {
  145. titleLab.font = UIFont.systemFont(ofSize: 11)
  146. } else {
  147. titleLab.font = UIFont.systemFont(ofSize: 14)
  148. }
  149. contentView.backgroundColor = ((bgmData as? PQStuckPointMusicTagsModel)?.isSelected ?? false) ? BFConfig.shared.styleBackGroundColor : BFConfig.shared.otherTintColor
  150. titleLab.textColor = ((bgmData as? PQStuckPointMusicTagsModel)?.isSelected ?? false) ? UIColor.hexColor(hexadecimal: BFConfig.shared.styleColor.rawValue) : BFConfig.shared.styleTitleColor
  151. remindView.isHidden = !((bgmData as? PQStuckPointMusicTagsModel)?.isSelected ?? false)
  152. }
  153. }
  154. func addLayout() {
  155. let margin: CGFloat = 12
  156. let leftmargin: CGFloat = 16
  157. let imageW: CGFloat = bgmData is PQVoiceModel ? 55 : 22
  158. if bgmData is PQVoiceModel {
  159. var nameW: CGFloat = sizeWithText(text: (bgmData as? PQVoiceModel)?.musicName ?? "", font: UIFont.systemFont(ofSize: 16), size: CGSize(width: frame.width - leftmargin - imageW - margin * 2, height: cDefaultMargin * 3)).width
  160. if nameW < cDefaultMargin * 3 {
  161. musicNameLab.text = "\((bgmData as? PQVoiceModel)?.musicName ?? "") "
  162. nameW = cDefaultMargin * 6
  163. } else if nameW < cDefaultMargin * 6 {
  164. musicNameLab.text = "\((bgmData as? PQVoiceModel)?.musicName ?? "") "
  165. nameW = cDefaultMargin * 6
  166. } else {
  167. musicNameLab.text = "\((bgmData as? PQVoiceModel)?.musicName ?? "") "
  168. }
  169. audioImageView.snp.remakeConstraints { make in
  170. make.left.equalToSuperview().offset(leftmargin)
  171. make.centerY.equalToSuperview()
  172. make.width.height.equalTo(imageW)
  173. }
  174. imageMaskView.snp.makeConstraints { make in
  175. make.size.equalToSuperview()
  176. }
  177. playImageView.snp.remakeConstraints { make in
  178. make.center.equalToSuperview()
  179. make.width.height.equalTo(cDefaultMargin * 2)
  180. }
  181. musicNameLab.snp.remakeConstraints { make in
  182. make.left.equalTo(audioImageView.snp.right).offset(margin)
  183. make.width.equalTo(nameW)
  184. make.height.equalTo(cDefaultMargin * 3)
  185. make.centerY.equalToSuperview()
  186. }
  187. confirmContentView.snp.remakeConstraints { make in
  188. make.right.equalToSuperview()
  189. make.centerY.equalToSuperview()
  190. make.width.equalTo(85)
  191. make.height.equalTo(55)
  192. }
  193. confirmBtn.snp.remakeConstraints { make in
  194. make.right.equalToSuperview().offset(-leftmargin)
  195. make.centerY.equalToSuperview()
  196. // make.width.equalTo(confirmBtn)
  197. make.height.equalTo(cDefaultMargin * 3)
  198. }
  199. audioImageView.addCorner(corner: imageW / 2)
  200. imageMaskView.addCorner(corner: imageW / 2)
  201. } else {
  202. audioImageView.snp.remakeConstraints { make in
  203. make.left.equalToSuperview().offset(cDefaultMargin)
  204. make.centerY.equalToSuperview()
  205. make.width.height.equalTo(imageW)
  206. }
  207. remindView.snp.makeConstraints { make in
  208. make.left.equalToSuperview().offset(-4)
  209. make.width.equalTo(8)
  210. make.height.equalTo(22)
  211. make.centerY.equalToSuperview()
  212. }
  213. titleLab.snp.remakeConstraints { make in
  214. make.left.equalTo(audioImageView.snp.right)
  215. make.right.equalToSuperview().offset(-margin)
  216. make.centerY.equalToSuperview()
  217. }
  218. }
  219. }
  220. @objc func confirmClick() {
  221. if btnClickHandle != nil {
  222. btnClickHandle!(confirmBtn, bgmData)
  223. }
  224. }
  225. func startLoadingAnimation(){
  226. let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
  227. rotateAnimation.toValue = Double.pi * 2
  228. rotateAnimation.duration = 1
  229. rotateAnimation.repeatCount = .infinity
  230. playImageView.layer.add(rotateAnimation, forKey: nil)
  231. }
  232. func stopLoadingAnimation(){
  233. playImageView.layer.removeAllAnimations()
  234. if (bgmData as? PQVoiceModel)?.isPlaying ?? false {
  235. playImageView.kf.setImage(with: URL(fileURLWithPath: currentBundlePath()!.path(forResource: "stuckPoint_music_playing", ofType: ".gif")!))
  236. }
  237. }
  238. }