forked from carabina/ProgressNode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressNode.swift
More file actions
198 lines (158 loc) · 7.01 KB
/
ProgressNode.swift
File metadata and controls
198 lines (158 loc) · 7.01 KB
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
//
// ProgressNode.swift
// SpriteKit extension
//
// Created by Tibor Bodecs on 06/02/15.
// Copyright (c) 2015 Tibor Bodecs. All rights reserved.
//
import SpriteKit
public class ProgressNode : SKShapeNode
{
///////////////////////////////////////////////////////////////////////////////////////////////////
// MARK: constants
///////////////////////////////////////////////////////////////////////////////////////////////////
struct Constants {
static let radius : CGFloat = 32
static let color : SKColor = SKColor.darkGrayColor()
static let backgroundColor : SKColor = SKColor.lightGrayColor()
static let width : CGFloat = 2.0
static let progress : CGFloat = 0.0
static let startAngle : CGFloat = CGFloat(M_PI_2)
static let clockwise : Bool = true
static private let actionKey = "_progressNodeCountdownActionKey"
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// MARK: properties
///////////////////////////////////////////////////////////////////////////////////////////////////
/// the radius of the progress node
public var radius: CGFloat = ProgressNode.Constants.radius {
didSet {
self.updateProgress(node: self.timeNode, progress: self.progress)
self.updateProgress(node: self)
}
}
//the active time color
public var color: SKColor = ProgressNode.Constants.color {
didSet {
self.timeNode.strokeColor = self.color
}
}
//the background color of the timer (to hide: use clear color)
public var backgroundColor: SKColor = ProgressNode.Constants.backgroundColor {
didSet {
self.strokeColor = self.backgroundColor
}
}
///the line width of the progress node
public var width: CGFloat = ProgressNode.Constants.width {
didSet {
self.timeNode.lineWidth = self.width
self.lineWidth = self.width
}
}
//the current progress of the progress node end progress is 1.0 and start is 0.0
public var progress: CGFloat = ProgressNode.Constants.progress {
didSet {
self.updateProgress(node: self.timeNode, progress: self.progress)
}
}
// the start angle of the progress node
public var startAngle: CGFloat = ProgressNode.Constants.startAngle {
didSet {
self.updateProgress(node: self.timeNode, progress: self.progress)
}
}
// the clockwise of the progress path
public var clockwise: Bool = ProgressNode.Constants.clockwise {
didSet {
self.updateProgress(node: self.timeNode, progress: self.progress)
self.updateProgress(node: self)
}
}
private let timeNode = SKShapeNode()
///////////////////////////////////////////////////////////////////////////////////////////////////
// MARK: init
///////////////////////////////////////////////////////////////////////////////////////////////////
public init(radius: CGFloat,
color: SKColor = ProgressNode.Constants.color,
backgroundColor: SKColor = ProgressNode.Constants.backgroundColor,
width: CGFloat = ProgressNode.Constants.width,
progress: CGFloat = ProgressNode.Constants.progress) {
self.radius = radius
self.color = color
self.backgroundColor = backgroundColor
self.width = width
self.progress = progress
super.init()
self._init()
}
override init() {
super.init()
self._init()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self._init()
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// MARK: helpers
///////////////////////////////////////////////////////////////////////////////////////////////////
private func _init() {
self.timeNode.lineWidth = self.width
self.timeNode.strokeColor = self.color
self.timeNode.zPosition = 10
self.timeNode.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(self.timeNode)
self.updateProgress(node: self.timeNode, progress: self.progress)
self.lineWidth = self.width
self.strokeColor = self.backgroundColor
self.zPosition = self.timeNode.zPosition
self.updateProgress(node: self)
}
private func updateProgress(#node: SKShapeNode, progress: CGFloat = 0.0) {
let progress = 1.0 - progress
let startAngle = CGFloat(M_PI / 2.0)
let endAngle = self.clockwise
? self.startAngle + progress * CGFloat(2.0*M_PI)
: self.startAngle - progress * CGFloat(2.0*M_PI)
node.path = UIBezierPath(arcCenter: CGPointZero,
radius: self.radius,
startAngle: self.startAngle,
endAngle: endAngle,
clockwise: self.clockwise).CGPath
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// MARK: API
///////////////////////////////////////////////////////////////////////////////////////////////////
/*!
The countdown method counts down from a time interval to zero,
and it calls a callback on the main thread if its finished
:param: time The time interval to count
:param: progressHandler An optional callback method (always called on main thread)
:param: callback An optional callback method (always called on main thread)
*/
public func countdown(time: NSTimeInterval = 1.0, completionHandler: ((Void) -> Void)?) {
self.countdown(time: time, progressHandler: nil, completionHandler: completionHandler)
}
public func countdown(time: NSTimeInterval = 1.0, progressHandler: ((Void) -> Void)?, completionHandler: ((Void) -> Void)?) {
self.stopCountdown()
self.runAction(SKAction.customActionWithDuration(time, actionBlock: {(node: SKNode!, elapsedTime: CGFloat) -> Void in
self.progress = elapsedTime / CGFloat(time)
if let cb = progressHandler {
dispatch_async(dispatch_get_main_queue(), {
cb()
})
}
if self.progress == 1.0 {
if let cb = completionHandler {
dispatch_async(dispatch_get_main_queue(), {
cb()
})
}
}
}), withKey:ProgressNode.Constants.actionKey)
}
public func stopCountdown() {
self.removeActionForKey(ProgressNode.Constants.actionKey)
}
}