PQStuckPointMusicContentCell.swift 9.9 KB

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