-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathMAGearRefreshControl.swift
executable file
·291 lines (207 loc) · 10.4 KB
/
MAGearRefreshControl.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//
// MAGearRefreshControl.swift
//
// Created by Michaël Azevedo on 20/05/2015.
// Copyright (c) 2015 micazeve. All rights reserved.
//
import UIKit
///MARK: - MAGearRefreshDelegate protocol
/// Protocol between the MAGearRefreshControl and its delegate (mostly UITableViewController).
@objc public protocol MAGearRefreshDelegate {
/// Method called when the pull to refresh move was triggered.
///
/// - parameter view: The MAGearRefreshControl object.
func MAGearRefreshTableHeaderDidTriggerRefresh(_ view:MAGearRefreshControl)
/// Method called to know if the data source is loading or no
///
/// - parameter view: The MAGearRefreshControl object.
///
/// - returns: true if the datasource is loading, false otherwise
func MAGearRefreshTableHeaderDataSourceIsLoading(_ view:MAGearRefreshControl) -> Bool
}
//MARK: - MAGearRefreshControl Class
/// This class is used to draw an animated group of gears and offers the same interactions as an UIRefreshControl
public class MAGearRefreshControl: MAAnimatedMultiGearView {
//MARK: Instance properties
/// Enum representing the different state of the refresh control
public enum MAGearRefreshState: UInt8 {
case normal // The user is pulling but hasn't reach the activation threshold yet
case pulling // The user is still pulling and has passed the activation threshold
case loading // The refresh control is animating
}
/// State of the refresh control
fileprivate var state = MAGearRefreshState.normal
/// Delegate conforming to the MAGearRefreshDelegate protocol. Most of time it's an UITableViewController
public var delegate:MAGearRefreshDelegate?
/// Content offset of the tableview
fileprivate var contentOffset:CGFloat = 0
/// Variable used to allow the end of the refresh
/// We must wait for the end of the animation of the contentInset before allowing the refresh
fileprivate var endRefreshAllowed = false
/// Variable used to know if the end of the refresh has been asked
fileprivate var endRefreshAsked = false
//MARK: Various methods
/// Set the state of the refresh control.
///
/// - parameter aState: New state of the refresh control.
fileprivate func setState(_ aState:MAGearRefreshState) {
NSLog("setState : \(aState.rawValue)")
switch aState {
case .loading:
self.rotate()
if style == .singleGear {
UIView.animate(withDuration: 0.5, animations: { () -> Void in
for i in 1..<self.arrayViews.count {
self.arrayViews[i].alpha = 0
} }, completion:nil)
}
break
default:
break
}
state = aState
}
//MARK: Public methods
/// Method to call when the scrollview was scrolled.
///
/// - parameter scrollView: The scrollview.
public func MAGearRefreshScrollViewDidScroll(_ scrollView:UIScrollView) {
configureWithContentOffsetY(-scrollView.contentOffset.y)
if (state == .loading) {
var offset = max(scrollView.contentOffset.y * -1, 0)
offset = min(offset, 60)
scrollView.contentInset = UIEdgeInsetsMake(offset, 0, 0, 0)
} else {
if (scrollView.isDragging) {
var loading = false
if let load = delegate?.MAGearRefreshTableHeaderDataSourceIsLoading(self) {
loading = load
}
if state == .pulling && scrollView.contentOffset.y > -65 && scrollView.contentOffset.y < 0 && !loading {
setState(.normal)
} else if state == .normal && scrollView.contentOffset.y < -65 && !loading {
setState(.pulling)
}
if (scrollView.contentInset.top != 0) {
scrollView.contentInset = UIEdgeInsets.zero;
}
}
let phase = -Double(scrollView.contentOffset.y/20)
if stopRotation {
setMainGearPhase(phase)
}
}
}
/// Method to call when the scrollview ended dragging
///
/// - parameter scrollView: The scrollview.
public func MAGearRefreshScrollViewDidEndDragging(_ scrollView:UIScrollView) {
NSLog("MAGearRefreshScrollViewDidEndDragging")
/*if state == .Loading {
NSLog("return")
return
}*/
var loading = false
if let load = delegate?.MAGearRefreshTableHeaderDataSourceIsLoading(self) {
loading = load
}
if scrollView.contentOffset.y <= -65.0 && !loading {
self.stopRotation = false
delegate?.MAGearRefreshTableHeaderDidTriggerRefresh(self)
setState(.loading)
let contentOffset = scrollView.contentOffset
UIView.animate(withDuration: 0.2, animations: { () -> Void in
scrollView.contentInset = UIEdgeInsetsMake(60, 0, 0, 0)
scrollView.contentOffset = contentOffset; // Workaround for smooth transition on iOS8
}, completion: { (completed) -> Void in
NSLog("completed")
let dispatchTime: DispatchTime = DispatchTime.now() + Double(Int64(0.6 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
DispatchQueue.main.asyncAfter(deadline: dispatchTime, execute: {
// your function here
self.endRefreshAllowed = true
if self.endRefreshAsked {
NSLog("self.endRefreshAsked")
self.endRefreshAsked = false
self.MAGearRefreshScrollViewDataSourceDidFinishedLoading(scrollView)
}
})
})
}
}
/// Method to call when the datasource finished loading
///
/// - parameter scrollView: The scrollview.
public func MAGearRefreshScrollViewDataSourceDidFinishedLoading(_ scrollView:UIScrollView) {
NSLog("MAGearRefreshScrollViewDataSourceDidFinishedLoading")
if !endRefreshAllowed {
endRefreshAsked = true
return
}
endRefreshAllowed = false
self.setState(.normal)
scrollView.isUserInteractionEnabled = false
UIView.animate(withDuration: 0.1, animations: { () -> Void in
self.arrayViews[0].transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
}, completion: { (finished) -> Void in
UIView.animate(withDuration: 0.3, animations: { () -> Void in
if self.style == .keepGears {
for i in 1..<self.arrayViews.count {
self.arrayViews[i].alpha = 0
}
}
self.arrayViews[0].transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
})
UIView.animate(withDuration: 0.3, delay: 0.1, options: .curveLinear, animations: { () -> Void in
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
scrollView.contentOffset = CGPoint(x: 0, y: 0); // Workaround for smooth transition on iOS8
}, completion: { (finished) -> Void in
self.stopRotation = true
scrollView.isUserInteractionEnabled = true
for view in self.arrayViews {
view.alpha = 1
view.transform = CGAffineTransform.identity
}
})
})
}
//MARK: View configuration
/// Method to configure the view with an Y offset of the scrollview
///
/// - parameter offset: Offset of the scrollView
fileprivate func configureWithContentOffsetY(_ offset:CGFloat)
{
contentOffset = offset
configureView()
}
/// Override of configureView(). The override is needed since we don't want the first gear to be centered within the view.
/// Instead, we want it to be centered within the visible part of the view
override internal func configureView() {
if arrayViews.count == 0 {
return
}
arrayViews[0].center.x = frame.size.width/2
arrayViews[0].center.y = frame.height - contentOffset/2
for i in 1..<arrayViews.count {
let angleBetweenGears = arrayAngles[i]
let gearView = arrayViews[i]
let gear = gearView.gear
let linkedGearView = arrayViews[arrayRelations[i]]
let linkedGear = linkedGearView.gear
let dist = Double((gear?.pitchDiameter)! + (linkedGear?.pitchDiameter)!)/2
let xValue = CGFloat(dist*cos(angleBetweenGears*Double.pi/180))
let yValue = CGFloat(-dist*sin(angleBetweenGears*Double.pi/180))
gearView.center = CGPoint(x: linkedGearView.center.x + xValue, y: linkedGearView.center.y + yValue)
arrayViews[i].gear = gear
}
leftBorderView.frame = CGRect(x: barMargin, y: frame.height - contentOffset, width: barWidth, height: contentOffset)
rightBorderView.frame = CGRect(x: frame.size.width - barMargin - barWidth, y: frame.height - contentOffset, width: barWidth, height: contentOffset)
}
//MARK: Public methods override
/// Override of startRotating in order to disable this portion of code (must be triggered from the tableview)
override public func startRotating() {
}
/// Override of stopRotating in order to disable this portion of code (must be triggered from delegate)
override public func stopRotating()
{
}
}