-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathProgressNode.swift
167 lines (134 loc) · 5.81 KB
/
ProgressNode.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
//
// 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
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// 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)
}
}
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 = startAngle+progress*CGFloat(2.0*M_PI)
node.path = UIBezierPath(arcCenter: CGPointZero,
radius: self.radius,
startAngle: startAngle,
endAngle: endAngle,
clockwise: true).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: callback An optional callback method (always called on main thread)
*/
public func countdown(time: NSTimeInterval = 1.0, callback: ((Void) -> Void)?) {
let actionKey = "_progressNodeTimeoutActionKey"
self.runAction(SKAction.repeatActionForever(SKAction.sequence([
SKAction.runBlock({ () -> Void in
var progress = self.progress+CGFloat(1/(30*time))
if progress > 1.0 {
progress = 1.0
self.removeActionForKey(actionKey)
if let cb = callback {
dispatch_async(dispatch_get_main_queue(), {
cb()
});
}
}
self.progress = CGFloat(progress)
}),
SKAction.waitForDuration(0.01)
])), withKey:actionKey)
}
}