KMToolSetScroller.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // KMToolSetScroller.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2022/10/27.
  6. //
  7. import Cocoa
  8. class KMToolSetScroller: NSScroller {
  9. var newScroolValue: Float = 0
  10. override var floatValue: Float {
  11. didSet {
  12. animator().alphaValue = 1.0
  13. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(fadeOut), object: nil)
  14. perform(#selector(fadeOut), with: nil, afterDelay: 1.5)
  15. }
  16. }
  17. override func draw(_ dirtyRect: NSRect) {
  18. super.draw(dirtyRect)
  19. self.drawKnob()
  20. }
  21. override func drawKnobSlot(in slotRect: NSRect, highlight flag: Bool) {
  22. }
  23. override init(frame frameRect: NSRect) {
  24. super.init(frame: frameRect)
  25. commonInitializer()
  26. }
  27. required init?(coder: NSCoder) {
  28. super.init(coder: coder)
  29. commonInitializer()
  30. }
  31. override func awakeFromNib() {
  32. super.awakeFromNib()
  33. commonInitializer()
  34. }
  35. // MARK:
  36. func commonInitializer() {
  37. let trackingArea: NSTrackingArea = NSTrackingArea(
  38. rect: self.bounds,
  39. options: [.mouseEnteredAndExited, .activeInActiveApp, .mouseMoved],
  40. owner: self,
  41. userInfo: nil
  42. )
  43. addTrackingArea(trackingArea)
  44. }
  45. @objc func fadeOut() {
  46. NSAnimationContext.runAnimationGroup({ (context) in
  47. context.duration = 0.3
  48. animator().alphaValue = 0.0
  49. }, completionHandler: nil)
  50. }
  51. override func mouseExited(with event: NSEvent) {
  52. super.mouseExited(with: event)
  53. fadeOut()
  54. }
  55. override func mouseEntered(with event: NSEvent) {
  56. super.mouseEntered(with: event)
  57. NSAnimationContext.runAnimationGroup({ (context) in
  58. context.duration = 0.1
  59. animator().alphaValue = 1.0
  60. }, completionHandler: nil)
  61. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(fadeOut), object: nil)
  62. }
  63. override func mouseMoved(with event: NSEvent) {
  64. super.mouseMoved(with: event)
  65. alphaValue = 1.0
  66. }
  67. override class var isCompatibleWithOverlayScrollers: Bool {
  68. return self == KMToolSetScroller.self
  69. }
  70. }