KeyboardCharacterCell.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // KeyboardCharacterCell.swift
  3. // AiKeyboard
  4. //
  5. // Created by Destiny on 2025/4/23.
  6. //
  7. import UIKit
  8. import Lottie
  9. import MarqueeLabel
  10. class KeyboardCharacterCell: UICollectionViewCell {
  11. static func reuseIdentifier() -> String {
  12. return "KeyboardCharacterCell"
  13. }
  14. var characterModel: CharacterModel?
  15. lazy var containerView: UIView = {
  16. let view = UIView()
  17. view.layer.cornerRadius = 9
  18. view.backgroundColor = .white
  19. return view
  20. }()
  21. lazy var titleLabel: MarqueeLabel = {
  22. let label = MarqueeLabel(frame: .zero, duration: 3.0, fadeLength: 0)
  23. label.type = .leftRight
  24. label.textAlignment = .center
  25. label.text = "💑 恋爱脑"
  26. label.font = .systemFont(ofSize: 14)
  27. label.textColor = .hexStringColor(hexString: "#000000").withAlphaComponent(0.8)
  28. return label
  29. }()
  30. lazy var addBtn: UIButton = {
  31. let btn = UIButton(frame: CGRect(x: 0, y: 0, width: KeyboardHelpView.UX.cellWidth, height: KeyboardHelpView.UX.cellHeight))
  32. btn.isEnabled = false
  33. btn.isHidden = true
  34. btn.setTitle("添加人设", for: .normal)
  35. btn.titleLabel?.font = .systemFont(ofSize: 14)
  36. btn.setTitleColor(.hexStringColor(hexString: "#7D46FC"), for: .normal)
  37. btn.setImage(UIImage(named: "icon_plus"), for: .normal)
  38. btn.setImageTitleLayout(.imgLeft, spacing: 3)
  39. return btn
  40. }()
  41. lazy var loadingAnimate: LottieAnimationView = {
  42. let animate = LottieAnimationView(name: "keyboard_loading")
  43. animate.isHidden = true
  44. animate.loopMode = .loop
  45. animate.backgroundColor = .clear
  46. return animate
  47. }()
  48. override init(frame: CGRect) {
  49. super.init(frame: frame)
  50. self.contentView.addSubview(containerView)
  51. containerView.snp.makeConstraints { make in
  52. make.edges.equalToSuperview()
  53. }
  54. self.containerView.addSubview(titleLabel)
  55. titleLabel.snp.makeConstraints { make in
  56. make.centerY.equalToSuperview()
  57. make.left.equalTo(4)
  58. make.right.equalTo(-4)
  59. }
  60. self.containerView.addSubview(addBtn)
  61. addBtn.snp.makeConstraints { make in
  62. make.edges.equalToSuperview()
  63. }
  64. self.containerView.addSubview(loadingAnimate)
  65. loadingAnimate.snp.makeConstraints { make in
  66. make.size.equalTo(CGSize(width: 32, height: 32))
  67. make.center.equalToSuperview()
  68. }
  69. }
  70. required init?(coder: NSCoder) {
  71. fatalError("init(coder:) has not been implemented")
  72. }
  73. func config(model: CharacterModel) {
  74. self.characterModel = model
  75. if let emoji = model.emoji, let name = model.name {
  76. self.titleLabel.text = "\(emoji) \(name)"
  77. }
  78. }
  79. func config(content: String) {
  80. self.titleLabel.text = "\(content)"
  81. }
  82. // 设置动画
  83. func setAnimate(play: Bool) {
  84. if play {
  85. self.titleLabel.isHidden = true
  86. self.loadingAnimate.isHidden = false
  87. self.loadingAnimate.play()
  88. } else {
  89. self.titleLabel.isHidden = false
  90. self.loadingAnimate.isHidden = true
  91. self.loadingAnimate.pause()
  92. }
  93. }
  94. }