KeyboardVipTipView.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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: 16, 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 unlockTextImageView: UIImageView = {
  56. let imageView = UIImageView()
  57. imageView.image = UIImage(named: "keyboard_vip_unlock_text")
  58. return imageView
  59. }()
  60. lazy var unlockLabel: UILabel = {
  61. // 注册自定义字体
  62. if let fontURL = Bundle.main.url(forResource: "tbmct", withExtension: "ttf") {
  63. CTFontManagerRegisterFontsForURL(fontURL as CFURL, .process, nil)
  64. }
  65. // let name = self.getFontPostScriptName(from: "淘宝买菜体.ttf")
  66. let label = UILabel()
  67. label.text = "¥0.1/天解锁"
  68. label.font = UIFont(name: "TBMCYXT", size: 24)
  69. label.textColor = .white
  70. return label
  71. }()
  72. override init(frame: CGRect) {
  73. super.init(frame: frame)
  74. setUI()
  75. }
  76. required init?(coder: NSCoder) {
  77. fatalError("init(coder:) has not been implemented")
  78. }
  79. func getFontPostScriptName(from fontFile: String) -> String? {
  80. guard let path = Bundle.main.path(forResource: fontFile, ofType: nil) else {
  81. return nil
  82. }
  83. let fontURL = URL(fileURLWithPath: path)
  84. guard let fontDataProvider = CGDataProvider(url: fontURL as CFURL),
  85. let font = CGFont(fontDataProvider) else {
  86. return nil
  87. }
  88. return font.postScriptName as String?
  89. }
  90. }
  91. extension KeyboardVipTipView {
  92. @objc func backButtonClickAction() {
  93. self.backBtnClosure?()
  94. }
  95. @objc func unlockBtnAction() {
  96. self.unlockBtnClosure?()
  97. }
  98. }
  99. extension KeyboardVipTipView {
  100. func setUI() {
  101. self.addSubview(bgImageView)
  102. bgImageView.snp.makeConstraints { make in
  103. make.edges.equalToSuperview()
  104. }
  105. self.addSubview(backBtn)
  106. backBtn.snp.makeConstraints { make in
  107. make.size.equalTo(CGSize(width: 32, height: 32))
  108. make.left.equalTo(12)
  109. make.top.equalTo(14)
  110. }
  111. self.addSubview(topImageView)
  112. topImageView.snp.makeConstraints { make in
  113. make.size.equalTo(CGSize(width: 264, height: 98.5))
  114. make.top.equalTo(47)
  115. make.centerX.equalToSuperview()
  116. }
  117. self.addSubview(mainView)
  118. mainView.snp.makeConstraints { make in
  119. make.size.equalTo(CGSize(width: 285, height: 122))
  120. make.centerX.equalToSuperview()
  121. make.top.equalTo(topImageView.snp.bottom).offset(-9)
  122. }
  123. mainView.addSubview(mainLabel)
  124. mainLabel.snp.makeConstraints { make in
  125. make.centerX.equalToSuperview()
  126. make.top.equalTo(21)
  127. }
  128. self.addSubview(unlockBtnAnimation)
  129. unlockBtnAnimation.snp.makeConstraints { make in
  130. make.size.equalTo(CGSize(width: 300, height: 48))
  131. make.centerX.equalToSuperview()
  132. make.top.equalTo(mainView.snp.bottom).offset(-30)
  133. }
  134. unlockBtnAnimation.play()
  135. unlockBtnAnimation.addSubview(unlockTextImageView)
  136. unlockTextImageView.snp.makeConstraints { make in
  137. make.center.equalToSuperview()
  138. }
  139. }
  140. }