KMLightMemberAlertView.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // KMLightMemberAlertView.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/2/24.
  6. //
  7. import Cocoa
  8. class KMLightMemberAlertView: KMBaseXibView {
  9. @IBOutlet weak var titleLabel: NSTextField!
  10. @IBOutlet weak var titleContentView: NSView!
  11. var timer: DispatchSourceTimer?
  12. var result: Result = Result() {
  13. didSet {
  14. self.alphaValue = 1.0
  15. self.reloadData()
  16. }
  17. }
  18. override func draw(_ dirtyRect: NSRect) {
  19. super.draw(dirtyRect)
  20. // Drawing code here.
  21. }
  22. override func setup() {
  23. super.setup()
  24. self.result = Result(code: 0)
  25. }
  26. override func updateUI() {
  27. super.updateUI()
  28. self.contentView.backgroundColor(NSColor(hex: "#FDC7C6"))
  29. self.titleLabel.font = NSFont.SFProTextRegular(14.0)
  30. self.titleLabel.textColor = NSColor(hex: "#252629")
  31. }
  32. override func reloadData() {
  33. super.reloadData()
  34. if result.code == 200 || result.code == 0 {
  35. self.contentView.isHidden = true
  36. } else {
  37. self.contentView.isHidden = false
  38. }
  39. self.titleLabel.stringValue = NSLocalizedString(KMRequestServerErrorCodeType.typeOfMessage(type: KMRequestServerErrorCodeType(rawValue: result.code)), comment: "")
  40. self.callMethodWithDelay()
  41. }
  42. func callMethodWithDelay() {
  43. // 取消之前的计时器
  44. timer?.cancel()
  45. // 创建一个新的计时器,延迟 2 秒
  46. timer = DispatchSource.makeTimerSource()
  47. timer?.schedule(deadline: .now() + 3, leeway: .milliseconds(10))
  48. timer?.setEventHandler(handler: { [unowned self] in
  49. // 2 秒后执行的代码
  50. methodToExecuteAfterDelay()
  51. })
  52. timer?.resume()
  53. }
  54. func methodToExecuteAfterDelay() {
  55. // 执行的代码
  56. self.fadeOut()
  57. }
  58. func fadeOut() {
  59. NSAnimationContext.runAnimationGroup({ (context) in
  60. context.duration = 1
  61. animator().alphaValue = 0.0
  62. }, completionHandler: nil)
  63. }
  64. override func updateLanguage() {
  65. super.updateLanguage()
  66. }
  67. }