ESRefreshComponent.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // ESRefreshComponent.swift
  3. //
  4. // Created by egg swift on 16/4/7.
  5. // Copyright (c) 2013-2016 ESPullToRefresh (https://github.com/eggswift/pull-to-refresh)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. import Foundation
  26. import UIKit
  27. public typealias ESRefreshHandler = (() -> ())
  28. open class ESRefreshComponent: UIView {
  29. open weak var scrollView: UIScrollView?
  30. /// @param handler Refresh callback method
  31. open var handler: ESRefreshHandler?
  32. /// @param animator Animated view refresh controls, custom must comply with the following two protocol
  33. open var animator: (ESRefreshProtocol & ESRefreshAnimatorProtocol)!
  34. /// @param refreshing or not
  35. fileprivate var _isRefreshing = false
  36. open var isRefreshing: Bool {
  37. get {
  38. return self._isRefreshing
  39. }
  40. }
  41. /// @param auto refreshing or not
  42. fileprivate var _isAutoRefreshing = false
  43. open var isAutoRefreshing: Bool {
  44. get {
  45. return self._isAutoRefreshing
  46. }
  47. }
  48. /// @param tag observing
  49. fileprivate var isObservingScrollView = false
  50. fileprivate var isIgnoreObserving = false
  51. public override init(frame: CGRect) {
  52. super.init(frame: frame)
  53. autoresizingMask = [.flexibleLeftMargin, .flexibleWidth, .flexibleRightMargin]
  54. }
  55. public convenience init(frame: CGRect, handler: @escaping ESRefreshHandler) {
  56. self.init(frame: frame)
  57. self.handler = handler
  58. self.animator = ESRefreshAnimator.init()
  59. }
  60. public convenience init(frame: CGRect, handler: @escaping ESRefreshHandler, animator: ESRefreshProtocol & ESRefreshAnimatorProtocol) {
  61. self.init(frame: frame)
  62. self.handler = handler
  63. self.animator = animator
  64. }
  65. public required init?(coder aDecoder: NSCoder) {
  66. fatalError("init(coder:) has not been implemented")
  67. }
  68. deinit {
  69. removeObserver()
  70. }
  71. open override func willMove(toSuperview newSuperview: UIView?) {
  72. super.willMove(toSuperview: newSuperview)
  73. /// Remove observer from superview immediately
  74. self.removeObserver()
  75. DispatchQueue.main.async { [weak self, newSuperview] in
  76. /// Add observer to new superview in next runloop
  77. self?.addObserver(newSuperview)
  78. }
  79. }
  80. open override func didMoveToSuperview() {
  81. super.didMoveToSuperview()
  82. self.scrollView = self.superview as? UIScrollView
  83. if let _ = animator {
  84. let v = animator.view
  85. if v.superview == nil {
  86. let inset = animator.insets
  87. self.addSubview(v)
  88. v.frame = CGRect.init(x: inset.left,
  89. y: inset.right,
  90. width: self.bounds.size.width - inset.left - inset.right,
  91. height: self.bounds.size.height - inset.top - inset.bottom)
  92. v.autoresizingMask = [
  93. .flexibleWidth,
  94. .flexibleTopMargin,
  95. .flexibleHeight,
  96. .flexibleBottomMargin
  97. ]
  98. }
  99. }
  100. }
  101. // MARK: - Action
  102. public final func startRefreshing(isAuto: Bool = false) -> Void {
  103. guard isRefreshing == false && isAutoRefreshing == false else {
  104. return
  105. }
  106. _isRefreshing = !isAuto
  107. _isAutoRefreshing = isAuto
  108. self.start()
  109. }
  110. public final func stopRefreshing() -> Void {
  111. guard isRefreshing == true || isAutoRefreshing == true else {
  112. return
  113. }
  114. self.stop()
  115. }
  116. public func start() {
  117. }
  118. public func stop() {
  119. _isRefreshing = false
  120. _isAutoRefreshing = false
  121. }
  122. // ScrollView contentSize change action
  123. public func sizeChangeAction(object: AnyObject?, change: [NSKeyValueChangeKey : Any]?) {
  124. }
  125. // ScrollView offset change action
  126. public func offsetChangeAction(object: AnyObject?, change: [NSKeyValueChangeKey : Any]?) {
  127. }
  128. }
  129. extension ESRefreshComponent /* KVO methods */ {
  130. fileprivate static var context = "ESRefreshKVOContext"
  131. fileprivate static let offsetKeyPath = "contentOffset"
  132. fileprivate static let contentSizeKeyPath = "contentSize"
  133. public func ignoreObserver(_ ignore: Bool = false) {
  134. if let scrollView = scrollView {
  135. scrollView.isScrollEnabled = !ignore
  136. }
  137. isIgnoreObserving = ignore
  138. }
  139. fileprivate func addObserver(_ view: UIView?) {
  140. if let scrollView = view as? UIScrollView, !isObservingScrollView {
  141. scrollView.addObserver(self, forKeyPath: ESRefreshComponent.offsetKeyPath, options: [.initial, .new], context: &ESRefreshComponent.context)
  142. scrollView.addObserver(self, forKeyPath: ESRefreshComponent.contentSizeKeyPath, options: [.initial, .new], context: &ESRefreshComponent.context)
  143. isObservingScrollView = true
  144. }
  145. }
  146. fileprivate func removeObserver() {
  147. if let scrollView = superview as? UIScrollView, isObservingScrollView {
  148. scrollView.removeObserver(self, forKeyPath: ESRefreshComponent.offsetKeyPath, context: &ESRefreshComponent.context)
  149. scrollView.removeObserver(self, forKeyPath: ESRefreshComponent.contentSizeKeyPath, context: &ESRefreshComponent.context)
  150. isObservingScrollView = false
  151. }
  152. }
  153. override open func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  154. if context == &ESRefreshComponent.context {
  155. guard isUserInteractionEnabled == true && isHidden == false else {
  156. return
  157. }
  158. if keyPath == ESRefreshComponent.contentSizeKeyPath {
  159. if isIgnoreObserving == false {
  160. sizeChangeAction(object: object as AnyObject?, change: change)
  161. }
  162. } else if keyPath == ESRefreshComponent.offsetKeyPath {
  163. if isIgnoreObserving == false {
  164. offsetChangeAction(object: object as AnyObject?, change: change)
  165. }
  166. }
  167. } else {
  168. }
  169. }
  170. }