TSTextToastView.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // TSTextToastView.swift
  3. // TSLiveWallpaper
  4. //
  5. // Created by 100Years on 2025/6/16.
  6. //
  7. public let kTextToastShared = TSTextToastView.shared
  8. open class TSTextToastView {
  9. static let shared = TSTextToastView()
  10. public var clickViewHandle:(()->Void)?
  11. private lazy var textLabel:UILabel = {
  12. let textLabel = UILabel()
  13. textLabel.textColor = UIColor.white
  14. textLabel.text = "Save Successfully".localized
  15. textLabel.font = UIFont.font(size: 14)
  16. return textLabel
  17. }()
  18. private lazy var saveSuccessBg: UIView = {
  19. return creatSaveSuccessBg()
  20. }()
  21. lazy var loadingView: UIImageView = {
  22. let loadingView = UIImageView.createImageView(image:.textToastLoading)
  23. return loadingView
  24. }()
  25. private lazy var viewButton:UIView = {
  26. let color = UIColor.themeColor
  27. let viewButton = UIButton.createButton(title: "View".localized ,backgroundColor: color.withAlphaComponent(0.2),font: UIFont.font(size: 12),titleColor: color,corner: 14) { [weak self] in
  28. guard let self = self else { return }
  29. if let clickViewHandle = clickViewHandle {
  30. clickViewHandle()
  31. }
  32. DispatchQueue.main.async{
  33. self.saveSuccessBg.removeFromSuperview()
  34. self.loadingView.stopRotating()
  35. }
  36. }
  37. viewButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
  38. return viewButton
  39. }()
  40. func creatSaveSuccessBg() -> UIView {
  41. let view = UIView()
  42. view.frame = CGRect(x: 0, y: 0, width: 288, height: 48)
  43. // 阴影
  44. view.backgroundColor = .clear
  45. view.layer.shadowColor = UIColor.black.cgColor
  46. view.layer.shadowOffset = CGSize(width: 0, height: 2)
  47. view.layer.shadowOpacity = 0.1
  48. // 圆角
  49. let colorBg = UIView()
  50. colorBg.backgroundColor = "#333333".uiColor
  51. colorBg.layer.cornerRadius = 8
  52. colorBg.layer.masksToBounds = true
  53. colorBg.clipsToBounds = true
  54. view.addSubview(colorBg)
  55. colorBg.snp.makeConstraints { make in
  56. make.leading.trailing.top.bottom.equalTo(0)
  57. }
  58. view.addSubview(loadingView)
  59. view.addSubview(viewButton)
  60. view.addSubview(textLabel)
  61. loadingView.snp.makeConstraints { make in
  62. make.width.height.equalTo(24)
  63. make.centerY.equalToSuperview()
  64. make.leading.equalTo(12)
  65. }
  66. viewButton.snp.makeConstraints { make in
  67. make.width.equalTo(viewButton.intrinsicContentSize.width)
  68. make.height.equalTo(28)
  69. make.trailing.equalTo(-8)
  70. make.centerY.equalToSuperview()
  71. }
  72. textLabel.snp.makeConstraints { make in
  73. make.leading.equalTo(loadingView.snp.trailing).offset(8)
  74. make.trailing.equalTo(viewButton.snp.leading).offset(-8)
  75. make.centerY.equalToSuperview()
  76. }
  77. return view
  78. }
  79. public func getBottom(topY:CGFloat)->CGFloat{
  80. let bottom = -(k_ScreenHeight - 48 - topY)
  81. debugPrint("bottom=\(bottom)")
  82. return bottom
  83. }
  84. public func show(atView:UIView,text:String = "Process successfully".localized,deadline:Double = 5.0,bottom:CGFloat = -112,showViewBtn:Bool = true,clickViewHandle:(()->Void)? = nil) {
  85. self.clickViewHandle = clickViewHandle
  86. kExecuteOnMainThread {
  87. self.textLabel.text = text
  88. self.viewButton.isHidden = !showViewBtn
  89. atView.addSubview(self.saveSuccessBg)
  90. self.saveSuccessBg.snp.remakeConstraints { make in
  91. make.width.greaterThanOrEqualTo(288)
  92. make.width.lessThanOrEqualTo(k_ScreenWidth-32)
  93. make.height.equalTo(48)
  94. make.centerX.equalToSuperview()
  95. make.top.equalTo(k_Nav_Height + 10)
  96. }
  97. self.loadingView.startRotating()
  98. }
  99. DispatchQueue.main.asyncAfter(deadline: .now() + deadline) {
  100. self.saveSuccessBg.removeFromSuperview()
  101. self.loadingView.stopRotating()
  102. }
  103. }
  104. }