24
24
import UIKit
25
25
import QuartzCore
26
26
27
- private var KVOContext = " RefresherKVOContext "
28
- private let ContentOffsetKeyPath = " contentOffset "
29
-
30
27
public enum PullToRefreshViewState {
31
28
32
29
case loading
@@ -43,7 +40,7 @@ public protocol PullToRefreshViewDelegate {
43
40
}
44
41
45
42
open class PullToRefreshView : UIView {
46
-
43
+ private var observation : NSKeyValueObservation ?
47
44
private var scrollViewBouncesDefaultValue : Bool = false
48
45
private var scrollViewInsetsDefaultValue : UIEdgeInsets = UIEdgeInsets . zero
49
46
@@ -101,51 +98,33 @@ open class PullToRefreshView: UIView {
101
98
// Currently it is not supported to load view from nib
102
99
}
103
100
104
- deinit {
105
- let scrollView = superview as? UIScrollView
106
- scrollView? . removeObserver ( self , forKeyPath: ContentOffsetKeyPath, context: & KVOContext)
107
- }
108
-
109
101
110
102
//MARK: UIView methods
111
103
112
104
open override func willMove( toSuperview newSuperview: UIView ! ) {
113
- superview ? . removeObserver ( self , forKeyPath : ContentOffsetKeyPath , context : & KVOContext )
105
+ self . observation ? . invalidate ( )
114
106
if let scrollView = newSuperview as? UIScrollView {
115
- scrollView. addObserver ( self , forKeyPath: ContentOffsetKeyPath, options: . initial, context: & KVOContext)
116
- scrollViewBouncesDefaultValue = scrollView. bounces
117
- scrollViewInsetsDefaultValue = scrollView. contentInset
118
- }
119
- }
120
-
121
-
122
- //MARK: KVO methods
123
-
124
- open override func observeValue( forKeyPath keyPath: String ? , of object: Any ? , change: [ NSKeyValueChangeKey : Any ] ? , context: UnsafeMutableRawPointer ? ) {
125
- if ( context == & KVOContext) {
126
- if let scrollView = superview as? UIScrollView , object as? NSObject == scrollView {
127
- if keyPath == ContentOffsetKeyPath {
128
- let offsetWithoutInsets = previousOffset + scrollViewInsetsDefaultValue. top
129
- if ( offsetWithoutInsets < - self . frame. size. height) {
130
- if ( scrollView. isDragging == false && loading == false ) {
131
- loading = true
132
- } else if ( loading) {
133
- self . animator. pullToRefresh ( self , stateDidChange: . loading)
134
- } else {
135
- self . animator. pullToRefresh ( self , stateDidChange: . releaseToRefresh)
136
- animator. pullToRefresh ( self , progressDidChange: - offsetWithoutInsets / self . frame. size. height)
137
- }
138
- } else if ( loading) {
107
+ self . observation = scrollView. observe ( \. contentOffset, options: [ . initial] ) { [ unowned self] ( scrollView, change) in
108
+ let offsetWithoutInsets = self . previousOffset + self . scrollViewInsetsDefaultValue. top
109
+ if ( offsetWithoutInsets < - self . frame. size. height) {
110
+ if ( scrollView. isDragging == false && self . loading == false ) {
111
+ self . loading = true
112
+ } else if ( self . loading) {
139
113
self . animator. pullToRefresh ( self , stateDidChange: . loading)
140
- } else if ( offsetWithoutInsets < 0 ) {
141
- self . animator. pullToRefresh ( self , stateDidChange: . pullToRefresh )
142
- animator. pullToRefresh ( self , progressDidChange: - offsetWithoutInsets / self . frame. size. height)
114
+ } else {
115
+ self . animator. pullToRefresh ( self , stateDidChange: . releaseToRefresh )
116
+ self . animator. pullToRefresh ( self , progressDidChange: - offsetWithoutInsets / self . frame. size. height)
143
117
}
144
- previousOffset = scrollView. contentOffset. y
118
+ } else if ( self . loading) {
119
+ self . animator. pullToRefresh ( self , stateDidChange: . loading)
120
+ } else if ( offsetWithoutInsets < 0 ) {
121
+ self . animator. pullToRefresh ( self , stateDidChange: . pullToRefresh)
122
+ self . animator. pullToRefresh ( self , progressDidChange: - offsetWithoutInsets / self . frame. size. height)
145
123
}
124
+ self . previousOffset = scrollView. contentOffset. y
146
125
}
147
- } else {
148
- super . observeValue ( forKeyPath : keyPath , of : object , change : change , context : context )
126
+ scrollViewBouncesDefaultValue = scrollView . bounces
127
+ scrollViewInsetsDefaultValue = scrollView . contentInset
149
128
}
150
129
}
151
130
0 commit comments