KeyboardCharacterCell.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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: 12)
  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.isHidden = true
  33. btn.setTitle("添加人设", for: .normal)
  34. btn.titleLabel?.font = .systemFont(ofSize: 12)
  35. btn.setTitleColor(.hexStringColor(hexString: "#7D46FC"), for: .normal)
  36. btn.setImage(UIImage(named: "icon_plus"), for: .normal)
  37. btn.setImageTitleLayout(.imgLeft, spacing: 3)
  38. return btn
  39. }()
  40. lazy var loadingAnimate: LottieAnimationView = {
  41. let animate = LottieAnimationView(name: "keyboard_loading")
  42. animate.isHidden = true
  43. animate.loopMode = .loop
  44. animate.backgroundColor = .clear
  45. return animate
  46. }()
  47. override init(frame: CGRect) {
  48. super.init(frame: frame)
  49. self.contentView.addSubview(containerView)
  50. containerView.snp.makeConstraints { make in
  51. make.edges.equalToSuperview()
  52. }
  53. self.containerView.addSubview(titleLabel)
  54. titleLabel.snp.makeConstraints { make in
  55. make.centerY.equalToSuperview()
  56. make.left.equalTo(4)
  57. make.right.equalTo(-4)
  58. }
  59. self.containerView.addSubview(addBtn)
  60. addBtn.snp.makeConstraints { make in
  61. make.edges.equalToSuperview()
  62. }
  63. self.containerView.addSubview(loadingAnimate)
  64. loadingAnimate.snp.makeConstraints { make in
  65. make.size.equalTo(CGSize(width: 32, height: 32))
  66. make.center.equalToSuperview()
  67. }
  68. }
  69. required init?(coder: NSCoder) {
  70. fatalError("init(coder:) has not been implemented")
  71. }
  72. func config(model: CharacterModel) {
  73. self.characterModel = model
  74. if let emoji = model.emoji, let name = model.name {
  75. self.titleLabel.text = "\(emoji) \(name)"
  76. }
  77. }
  78. func config(content: String) {
  79. self.titleLabel.text = "\(content)"
  80. }
  81. // 设置动画
  82. func setAnimate(play: Bool) {
  83. if play {
  84. self.titleLabel.isHidden = true
  85. self.loadingAnimate.isHidden = false
  86. self.loadingAnimate.play()
  87. } else {
  88. self.titleLabel.isHidden = false
  89. self.loadingAnimate.isHidden = true
  90. self.loadingAnimate.pause()
  91. }
  92. }
  93. }