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