KeyboardVipTipView.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // KeyboardVipTipView.swift
  3. // AiKeyboard
  4. //
  5. // Created by Destiny on 2025/4/25.
  6. //
  7. import UIKit
  8. import Lottie
  9. class KeyboardVipTipView: UIView {
  10. var unlockBtnClosure: (() -> ())?
  11. var backBtnClosure: (() -> ())?
  12. lazy var bgImageView: UIImageView = {
  13. let imageView = UIImageView()
  14. imageView.image = UIImage(named: "keyboard_bg")
  15. return imageView
  16. }()
  17. lazy var backBtn: UIButton = {
  18. let btn = UIButton()
  19. btn.setImage(UIImage(named: "keyboard_back_btn"), for: .normal)
  20. btn.layer.shadowOffset = CGSize(width: 0, height: 0)
  21. btn.layer.shadowColor = UIColor.hexStringColor(hexString: "#000000", alpha: 0.12).cgColor
  22. btn.layer.shadowRadius = 6.1
  23. btn.addTarget(self, action: #selector(backButtonClickAction), for: .touchUpInside)
  24. return btn
  25. }()
  26. lazy var topImageView: UIImageView = {
  27. let imageView = UIImageView()
  28. imageView.image = UIImage(named: "keyboard_vip_top")
  29. return imageView
  30. }()
  31. lazy var mainView: UIView = {
  32. let view = UIView()
  33. view.backgroundColor = .white
  34. view.layer.cornerRadius = 20
  35. return view
  36. }()
  37. lazy var mainLabel: UILabel = {
  38. let label = UILabel()
  39. label.numberOfLines = 2
  40. label.text = "您的免费提问次数已用完\n可订阅会员解锁无线提问"
  41. label.font = .systemFont(ofSize: 14, weight: .medium)
  42. label.textColor = .hexStringColor(hexString: "#000000", alpha: 0.8)
  43. label.setLineSpacing(5)
  44. return label
  45. }()
  46. lazy var unlockBtnAnimation: LottieAnimationView = {
  47. let animate = LottieAnimationView(name: "vip_button")
  48. animate.loopMode = .loop
  49. animate.backgroundColor = .clear
  50. animate.isUserInteractionEnabled = true
  51. let tap = UITapGestureRecognizer(target: self, action: #selector(unlockBtnAction))
  52. animate.addGestureRecognizer(tap)
  53. return animate
  54. }()
  55. lazy var unlockLabel: UILabel = {
  56. // 注册自定义字体
  57. if let fontURL = Bundle.main.url(forResource: "tbmct", withExtension: "ttf") {
  58. CTFontManagerRegisterFontsForURL(fontURL as CFURL, .process, nil)
  59. }
  60. // let name = self.getFontPostScriptName(from: "淘宝买菜体.ttf")
  61. let label = UILabel()
  62. label.text = "¥0.1/天解锁"
  63. label.font = UIFont(name: "TBMCYXT", size: 24)
  64. label.textColor = .white
  65. return label
  66. }()
  67. override init(frame: CGRect) {
  68. super.init(frame: frame)
  69. setUI()
  70. }
  71. required init?(coder: NSCoder) {
  72. fatalError("init(coder:) has not been implemented")
  73. }
  74. func getFontPostScriptName(from fontFile: String) -> String? {
  75. guard let path = Bundle.main.path(forResource: fontFile, ofType: nil) else {
  76. return nil
  77. }
  78. let fontURL = URL(fileURLWithPath: path)
  79. guard let fontDataProvider = CGDataProvider(url: fontURL as CFURL),
  80. let font = CGFont(fontDataProvider) else {
  81. return nil
  82. }
  83. return font.postScriptName as String?
  84. }
  85. }
  86. extension KeyboardVipTipView {
  87. @objc func backButtonClickAction() {
  88. self.backBtnClosure?()
  89. }
  90. @objc func unlockBtnAction() {
  91. self.unlockBtnClosure?()
  92. }
  93. }
  94. extension KeyboardVipTipView {
  95. func setUI() {
  96. self.addSubview(bgImageView)
  97. bgImageView.snp.makeConstraints { make in
  98. make.edges.equalToSuperview()
  99. }
  100. self.addSubview(backBtn)
  101. backBtn.snp.makeConstraints { make in
  102. make.size.equalTo(CGSize(width: 32, height: 32))
  103. make.left.equalTo(12)
  104. make.top.equalTo(14)
  105. }
  106. self.addSubview(topImageView)
  107. topImageView.snp.makeConstraints { make in
  108. make.size.equalTo(CGSize(width: 264, height: 98.5))
  109. make.top.equalTo(47)
  110. make.centerX.equalToSuperview()
  111. }
  112. self.addSubview(mainView)
  113. mainView.snp.makeConstraints { make in
  114. make.size.equalTo(CGSize(width: 285, height: 122))
  115. make.centerX.equalToSuperview()
  116. make.top.equalTo(topImageView.snp.bottom).offset(-9)
  117. }
  118. mainView.addSubview(mainLabel)
  119. mainLabel.snp.makeConstraints { make in
  120. make.centerX.equalToSuperview()
  121. make.top.equalTo(21)
  122. }
  123. self.addSubview(unlockBtnAnimation)
  124. unlockBtnAnimation.snp.makeConstraints { make in
  125. make.size.equalTo(CGSize(width: 300, height: 48))
  126. make.centerX.equalToSuperview()
  127. make.top.equalTo(mainView.snp.bottom).offset(-30)
  128. }
  129. unlockBtnAnimation.play()
  130. unlockBtnAnimation.addSubview(unlockLabel)
  131. unlockLabel.snp.makeConstraints { make in
  132. make.center.equalToSuperview()
  133. }
  134. }
  135. }