-
Notifications
You must be signed in to change notification settings - Fork 14
/
GIFRefreshControl.swift
361 lines (278 loc) · 11 KB
/
GIFRefreshControl.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//
// GIFRefreshControl.swift
// GIFRefreshControl
//
// Created by Kevin DELANNOY on 16/05/15.
// Copyright (c) 2015 Kevin Delannoy. All rights reserved.
//
import UIKit
import ImageIO
// MARK: - AnimatedImage
////////////////////////////////////////////////////////////////////////////
@objc public protocol AnimatedImage {
var size: CGSize { get }
var frameCount: UInt { get }
func frameDurationForImage(at index: UInt) -> TimeInterval
subscript(index: UInt) -> UIImage { get }
}
////////////////////////////////////////////////////////////////////////////
// MARK: - GIFAnimatedImage
////////////////////////////////////////////////////////////////////////////
public class GIFAnimatedImage: NSObject, AnimatedImage {
fileprivate typealias ImageInfo = (image: UIImage, duration: TimeInterval)
fileprivate let images: [ImageInfo]
public let size: CGSize
public var frameCount: UInt {
return UInt(images.count)
}
public init?(data: Data) {
guard let source = CGImageSourceCreateWithData(data as CFData, nil) else {
images = []
size = .zero
super.init()
return nil
}
let count = CGImageSourceGetCount(source)
images = (0..<count).map { i -> ImageInfo in
guard let image = CGImageSourceCreateImageAtIndex(source, i, nil) else {
return (UIImage(), 0)
}
let duration: TimeInterval = {
let info = CGImageSourceCopyPropertiesAtIndex(source, i, nil)
let gifInfo = unsafeBitCast(CFDictionaryGetValue(info, Unmanaged.passUnretained(kCGImagePropertyGIFDictionary).toOpaque()), to: CFDictionary.self)
var delay = unsafeBitCast(CFDictionaryGetValue(gifInfo, Unmanaged.passUnretained(kCGImagePropertyGIFUnclampedDelayTime).toOpaque()), to: NSNumber.self)
if delay.doubleValue <= 0 {
delay = unsafeBitCast(CFDictionaryGetValue(gifInfo, Unmanaged.passUnretained(kCGImagePropertyGIFDelayTime).toOpaque()), to: NSNumber.self)
}
return delay.doubleValue
}()
return (UIImage(cgImage: image), duration)
}
if let image = images.first {
size = image.image.size
} else {
size = CGSize(width: 0, height: 100)
}
super.init()
}
public func frameDurationForImage(at index: UInt) -> TimeInterval {
return images[Int(index)].duration
}
public subscript(index: UInt) -> UIImage {
return images[Int(index)].image
}
}
////////////////////////////////////////////////////////////////////////////
// MARK: - GIFAnimatedImageView
////////////////////////////////////////////////////////////////////////////
private class GIFAnimatedImageView: UIImageView {
var animatedImage: AnimatedImage? {
didSet {
image = animatedImage?[0]
}
}
var animated = false
var lastTimestampChange = CFTimeInterval(0)
lazy var displayLink: CADisplayLink = {
let dl = CADisplayLink(target: self, selector: #selector(GIFAnimatedImageView.refreshDisplay))
dl.add(to: .main, forMode: .commonModes)
dl.isPaused = true
return dl
}()
var index = UInt(0) {
didSet {
if index != oldValue {
image = animatedImage?[index]
}
}
}
override func startAnimating() {
if !animated {
displayLink.isPaused = false
animated = true
}
}
@objc func refreshDisplay() {
if animated {
if let animatedImage = animatedImage {
let currentFrameDuration = animatedImage.frameDurationForImage(at: index)
let delta = displayLink.timestamp - lastTimestampChange
if delta >= currentFrameDuration {
index = (index + 1) % animatedImage.frameCount
lastTimestampChange = displayLink.timestamp
}
}
}
}
override func stopAnimating() {
if animated {
displayLink.isPaused = true
animated = false
}
}
}
////////////////////////////////////////////////////////////////////////////
// MARK: - GIFRefreshControl
////////////////////////////////////////////////////////////////////////////
public class GIFRefreshControl: UIControl {
private var imageView = GIFAnimatedImageView()
private var forbidsOffsetChanges = false
private var forbidsInsetChanges = false
private var changingInset = false
private var refreshing = false
private var contentInset: UIEdgeInsets?
// MARK: Initialization
////////////////////////////////////////////////////////////////////////////
public convenience init() {
self.init(frame: .zero)
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
public override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
private func commonInit() {
imageView.frame = bounds
imageView.clipsToBounds = true
imageView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
addSubview(imageView)
}
deinit {
imageView.stopAnimating()
if let superview = superview as? UIScrollView {
superview.removeObserver(self, forKeyPath: "contentOffset")
}
}
////////////////////////////////////////////////////////////////////////////
// MARK: Superview handling
////////////////////////////////////////////////////////////////////////////
public override func willMove(toSuperview newSuperview: UIView?) {
if let superview = superview as? UIScrollView {
superview.removeObserver(self, forKeyPath: "contentOffset")
}
super.willMove(toSuperview: newSuperview)
}
public override func didMoveToSuperview() {
if let superview = superview as? UIScrollView {
superview.addObserver(self, forKeyPath: "contentOffset", options: [.new, .old], context: nil)
}
super.didMoveToSuperview()
}
////////////////////////////////////////////////////////////////////////////
// MARK: Configuration
////////////////////////////////////////////////////////////////////////////
public var animatedImage: AnimatedImage? {
get {
return imageView.animatedImage
}
set {
imageView.animatedImage = newValue
}
}
public override var contentMode: UIViewContentMode {
get {
return imageView.contentMode
}
set {
imageView.contentMode = newValue
}
}
public var animateOnScroll = true
public var animationDuration = TimeInterval(0.33)
public var animationDamping = CGFloat(0.4)
public var animationVelocity = CGFloat(0.8)
////////////////////////////////////////////////////////////////////////////
// MARK: Refresh methods
////////////////////////////////////////////////////////////////////////////
public func beginRefreshing() {
if let superview = superview as? UIScrollView, !refreshing {
refreshing = true
//Saving inset
contentInset = superview.contentInset
let currentOffset = superview.contentOffset
//Setting new inset
changingInset = true
var inset = superview.contentInset
inset.top = inset.top + expandedHeight
superview.contentInset = inset
changingInset = false
//Aaaaand scrolling
superview.setContentOffset(currentOffset, animated: false)
superview.setContentOffset(CGPoint(x: 0, y: -inset.top), animated: true)
forbidsOffsetChanges = true
}
}
public func endRefreshing() {
if let superview = superview as? UIScrollView, refreshing {
forbidsOffsetChanges = false
refreshing = false
UIView.animate(withDuration: animationDuration,
delay: 0,
usingSpringWithDamping: animationDamping,
initialSpringVelocity: animationVelocity,
options: UIViewAnimationOptions.curveLinear,
animations: { () -> Void in
if let contentInset = self.contentInset {
superview.contentInset = contentInset
self.contentInset = nil
}
}) { (finished) -> Void in
self.imageView.stopAnimating()
self.imageView.index = 0
}
}
}
////////////////////////////////////////////////////////////////////////////
// MARK: KVO
////////////////////////////////////////////////////////////////////////////
public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if !changingInset {
adaptShift()
}
}
////////////////////////////////////////////////////////////////////////////
// MARK: Shift
////////////////////////////////////////////////////////////////////////////
private var expandedHeight: CGFloat {
let maxHeight = UIScreen.main.bounds.height / 5
let height = imageView.animatedImage?.size.height
return min(maxHeight, height ?? maxHeight)
}
private func adaptShift() {
if let superview = superview as? UIScrollView {
//Updating frame
let topInset = (contentInset ?? superview.contentInset).top
let originY = superview.contentOffset.y + topInset
let height = originY
frame = CGRect(origin: CGPoint(x: 0, y: originY),
size: CGSize(width: superview.frame.width, height: -height))
//Detecting refresh gesture
if superview.contentOffset.y + topInset <= -expandedHeight {
forbidsInsetChanges = true
}
else {
if !refreshing {
forbidsInsetChanges = false
}
}
//We cannot do this in the previous if/else because then some frames
//might not be drawn
if animateOnScroll && !refreshing && superview.contentOffset.y + topInset < 0 {
let percentage = min(1, fabs(height) / expandedHeight)
let count = CGFloat(imageView.animatedImage?.frameCount ?? 1) - 1
let index = UInt(count * percentage)
imageView.index = index
}
if !superview.isDragging && superview.isDecelerating && !forbidsOffsetChanges && forbidsInsetChanges {
imageView.startAnimating()
sendActions(for: .valueChanged)
beginRefreshing()
}
}
}
////////////////////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////////