-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathCustomInfiniteIndicator.swift
194 lines (155 loc) · 4.94 KB
/
CustomInfiniteIndicator.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
//
// CustomInfiniteIndicator.swift
// InfiniteScrollViewDemoSwift
//
// Created by pronebird on 5/3/15.
// Copyright (c) 2015 pronebird. All rights reserved.
//
import UIKit
private let rotationAnimationKey = "rotation"
class CustomInfiniteIndicator: UIView {
var thickness: CGFloat = 2
var outerColor: UIColor? {
didSet {
updateColors()
}
}
var innerColor: UIColor? {
didSet {
updateColors()
}
}
private var animating = false
private let innerCircle = CAShapeLayer()
private let outerCircle = CAShapeLayer()
// MARK: - Public
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
deinit {
unregisterFromAppStateNotifications()
}
override func layoutSublayers(of layer: CALayer) {
super.layoutSublayers(of: layer)
setupBezierPaths()
}
override func didMoveToWindow() {
super.didMoveToWindow()
if let _ = window {
restartAnimationIfNeeded()
}
}
override func tintColorDidChange() {
if innerColor == nil {
updateColors()
}
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if #available(iOS 13.0, tvOS 13.0, *) {
if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
updateColors()
}
}
}
func isAnimating() -> Bool {
return animating
}
@objc func startAnimating() {
guard !animating else { return }
animating = true
isHidden = false
addAnimation()
}
@objc func stopAnimating() {
guard animating else { return }
animating = false
isHidden = true
removeAnimation()
}
// MARK: - Private
private func commonInit() {
registerForAppStateNotifications()
isHidden = true
backgroundColor = UIColor.clear
outerCircle.fillColor = UIColor.clear.cgColor
outerCircle.lineWidth = thickness
innerCircle.fillColor = UIColor.clear.cgColor
innerCircle.lineWidth = thickness
updateColors()
layer.addSublayer(outerCircle)
layer.addSublayer(innerCircle)
}
private func updateColors() {
let outerColor = outerColor ?? defaultOuterColor()
let innerColor = innerColor ?? tintColor
outerCircle.strokeColor = outerColor.cgColor
innerCircle.strokeColor = innerColor?.cgColor
}
private func addAnimation() {
let anim = animation()
anim.timeOffset = layer.convertTime(CACurrentMediaTime(), from: nil)
layer.add(anim, forKey: rotationAnimationKey)
}
private func removeAnimation() {
layer.removeAnimation(forKey: rotationAnimationKey)
}
@objc func restartAnimationIfNeeded() {
let anim = layer.animation(forKey: rotationAnimationKey)
if animating, anim == nil {
removeAnimation()
addAnimation()
}
}
private func registerForAppStateNotifications() {
NotificationCenter.default.addObserver(
self,
selector: #selector(CustomInfiniteIndicator.restartAnimationIfNeeded),
name: UIApplication.willEnterForegroundNotification,
object: nil
)
}
private func unregisterFromAppStateNotifications() {
NotificationCenter.default.removeObserver(self)
}
private func animation() -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: "transform.rotation")
animation.toValue = NSNumber(value: Double.pi * 2)
animation.duration = 1
animation.repeatCount = Float.infinity
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
return animation
}
private func setupBezierPaths() {
let center = CGPoint(x: bounds.size.width * 0.5, y: bounds.size.height * 0.5)
let radius = bounds.size.width * 0.5 - thickness
let ringPath = UIBezierPath(
arcCenter: center,
radius: radius,
startAngle: CGFloat(0),
endAngle: CGFloat.pi * 2,
clockwise: true
)
let quarterRingPath = UIBezierPath(
arcCenter: center,
radius: radius,
startAngle: -CGFloat.pi / 4,
endAngle: CGFloat.pi / 2 - CGFloat.pi / 4,
clockwise: true
)
outerCircle.path = ringPath.cgPath
innerCircle.path = quarterRingPath.cgPath
}
private func defaultOuterColor() -> UIColor {
if #available(iOS 13.0, tvOS 13, *) {
return .systemGray4
} else {
return .gray.withAlphaComponent(0.2)
}
}
}