KMAotuFlowExtension.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // KMAotuFlowExtension.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2024/1/16.
  6. //
  7. import Cocoa
  8. private var autoFlowTimerKey: UInt8 = 0
  9. private var fastFlowTimerKey: UInt8 = 0
  10. extension CPDFView {
  11. var autoFlowTimer: Timer? {
  12. get {
  13. return objc_getAssociatedObject(self, &autoFlowTimerKey) as? Timer
  14. }
  15. set {
  16. objc_setAssociatedObject(self, &autoFlowTimerKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  17. }
  18. }
  19. var fastFlowTimer: Timer? {
  20. get {
  21. return objc_getAssociatedObject(self, &fastFlowTimerKey) as? Timer
  22. }
  23. set {
  24. objc_setAssociatedObject(self, &fastFlowTimerKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  25. }
  26. }
  27. func startAutoFlow() {
  28. let timeInterval = KMAutoFlowOptionsSheetController.timeInterval()
  29. let timer = Timer.scheduledTimer(timeInterval: TimeInterval(timeInterval), target: self, selector: #selector(updateAutoFlow), userInfo: nil, repeats: true)
  30. autoFlowTimer = timer
  31. CustomAlertView.alertView(message: NSLocalizedString("AutoScroll On", comment: ""), fromView: self, withStyle: .black)
  32. }
  33. @objc func updateAutoFlow() {
  34. self.setDisplay(.singlePage)
  35. let jumpSpace = KMAutoFlowOptionsSheetController.jumpSpace()
  36. // if KMPreferenceManager.shared.viewPageDisplayType == .singlePage ||
  37. // KMPreferenceManager.shared.viewPageDisplayType == .twoUp {
  38. // if canGoToNextPage() {
  39. // goToNextPage(nil)
  40. // } else {
  41. // stopAutoFlow()
  42. // }
  43. // } else {
  44. // if let scrollView = documentView() {
  45. // let clipView = scrollView.contentView
  46. // var newOrigin = clipView.bounds.origin
  47. // newOrigin.y = newOrigin.y + CGFloat(jumpSpace)
  48. // clipView.animator().setBoundsOrigin(newOrigin)
  49. //
  50. // if newOrigin.y <= 0 {
  51. // stopAutoFlow()
  52. // }
  53. // }
  54. // }
  55. }
  56. func stopAutoFlow() {
  57. autoFlowTimer?.invalidate()
  58. autoFlowTimer = nil
  59. CustomAlertView.alertView(message: NSLocalizedString("AutoScroll Off", comment: ""), fromView: self, withStyle: .black)
  60. }
  61. func isAutoFlow() -> Bool {
  62. return autoFlowTimer != nil && autoFlowTimer!.isValid
  63. }
  64. func autoFlow() {
  65. if isAutoFlow() {
  66. stopAutoFlow()
  67. } else {
  68. startAutoFlow()
  69. }
  70. }
  71. func startFastFlow() {
  72. let timeInterval: TimeInterval = 0.1
  73. let timer = Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(updateFastFlow), userInfo: nil, repeats: true)
  74. fastFlowTimer = timer
  75. }
  76. @objc func updateFastFlow() {
  77. let jumpSpace: CGFloat = 200
  78. // if !isScrollUp {
  79. // // jumpSpace为负值
  80. // let negativeJumpSpace = -jumpSpace
  81. // updateScrollView(with: negativeJumpSpace)
  82. // } else {
  83. updateScrollView(with: jumpSpace)
  84. // }
  85. }
  86. func updateScrollView(with jumpSpace: CGFloat) {
  87. if self.displayMode() == .singlePage || self.displayMode() == .twoUp {
  88. if canGoToNextPage() {
  89. goToNextPage(nil)
  90. } else {
  91. stopFastFlow()
  92. }
  93. } else {
  94. if let scrollView = documentView().enclosingScrollView {
  95. let clipView = scrollView.contentView
  96. var newOrigin = clipView.bounds.origin
  97. newOrigin.y -= jumpSpace
  98. clipView.animator().setBoundsOrigin(newOrigin)
  99. if newOrigin.y <= 0 /*&& isScrollUp*/ {
  100. stopFastFlow()
  101. } else if newOrigin.y >= documentView().frame.size.height /*&& !isScrollUp*/ {
  102. stopFastFlow()
  103. }
  104. }
  105. }
  106. }
  107. func stopFastFlow() {
  108. fastFlowTimer?.invalidate()
  109. fastFlowTimer = nil
  110. }
  111. func isFastFlow() -> Bool {
  112. return fastFlowTimer != nil && fastFlowTimer!.isValid
  113. }
  114. func fastFlow() {
  115. if isFastFlow() {
  116. stopFastFlow()
  117. } else {
  118. startFastFlow()
  119. }
  120. }
  121. }