-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCircleSlider.swift
326 lines (257 loc) · 12 KB
/
CircleSlider.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
// Nicholas Ragonese
// May 13, 2016
// github.com/nichrago
// Version 1.0
// -----------
// Basically, a slide controller in a circle shape.
// ------------------------------------------------
// Hello dear developer,
// -------------------------
// Set all the properties you want to edit and then call makeSlider() because that
// way it will work.
//
// You can set the sizes and colors of the objects and can set your custom selectors for
// touchesMoved, touchFailed, circleCompleted. the moved selector will send back the
// radian but keep in mind it us upside down to algebra conventions. Well actually since
// the y coordinate is upside down in the first place it is really upside up but whatever.
//
// If you default all the properties then an average size will be set for all the size
// properties and the colors will be different shades of grey.
//
// There is a get variable that returns a view in the shape of the inner circle.
//
// There is a bool property where if true when the circle is completed the view dissapears
// called dissappear_when_completed which is fale by default.
import UIKit
class CircleSlider: UIView {
// * editable properties *
// sizes
var circle_diameter: CGFloat!
var circle_width: CGFloat!
var touch_diameter: CGFloat! // from end to end of the actual line
var touch_tolerance: CGFloat! // number of pixels +/- off the circle line that the touch will register
// colors
var circle_color = UIColor.darkGrayColor()
var touch_color = UIColor.grayColor()
var trail_color = UIColor.lightGrayColor()
// for ease of use
var dissappear_on_completion = false
// selectors for actions, you know because this is a legit class
enum CircleSliderAction { case touchMoved, touchFailed, circleCompleted }
private var moved_target: NSObject?
private var moved_selector: Selector?
private var failed_target: NSObject?
private var failed_selector: Selector?
private var completed_target: NSObject?
private var completed_selector: Selector?
func setSelector(forAction action: CircleSliderAction, target: NSObject, selector: Selector) {
switch action {
case .touchMoved:
moved_target = target
moved_selector = selector
case .touchFailed:
failed_target = target
failed_selector = selector
case .circleCompleted:
completed_target = target
completed_selector = selector
}
}
// * global variables for the class *
// circle draw objects
private var drawn_circle: UIBezierPath!
private var inner_circle: UIBezierPath!
private var outer_circle: UIBezierPath!
// touching draw objects
private var touch_circle: UIBezierPath!
private var touch_trail: UIBezierPath!
// circle maths things
private var circle_center: CGPoint!
private var start_rad: CGFloat?
// testing variables
private var start_point: CGPoint?
private var clockwise: Bool?
private var circled = false
private var left_check: CGPoint?
private var right_check: CGPoint?
// * Get variables for convenience and delight *
var innerView: UIView? {
// return a view in the shape and size of the inner circle
get {
guard (circle_diameter != nil) && (circle_width != nil) && (circle_center != nil) else {
return nil
}
let innerView = UIView(frame: CGRectMake(0, 0, circle_diameter! - circle_width!, circle_diameter! - circle_width!))
innerView.layer.cornerRadius = innerView.frame.size.width / 2
innerView.center = CGPointMake(circle_center.x + self.frame.origin.x, circle_center.y + self.frame.origin.y)
return innerView
}
}
// * Functions to start the things *
func makeSlider() -> Bool {
// prep and draw the circle, returns false if the view is too small for the properties
self.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.0)
if circle_width == nil { circle_width = self.frame.width / 10 }
if touch_diameter == nil { touch_diameter = self.frame.width / 8 }
if touch_tolerance == nil { touch_tolerance = self.frame.width / 7 }
if circle_diameter == nil { circle_diameter = self.frame.width - (touch_tolerance * 2) }
if (circle_diameter + (touch_tolerance * 2)) > (self.frame.width) { return false }
circle_center = CGPointMake(self.frame.width / 2, self.frame.height / 2)
drawn_circle = UIBezierPath(arcCenter: circle_center, radius: (circle_diameter / 2),
startAngle: 0, endAngle: CGFloat(M_PI * 2), clockwise: true)
drawn_circle.lineWidth = circle_width
outer_circle = UIBezierPath(arcCenter: circle_center, radius: (circle_diameter / 2) + touch_tolerance,
startAngle: 0, endAngle: CGFloat(M_PI * 2), clockwise: true)
inner_circle = UIBezierPath(arcCenter: circle_center, radius: (circle_diameter / 2) - touch_tolerance,
startAngle: 0, endAngle: CGFloat(M_PI * 2), clockwise: true)
self.setNeedsDisplay()
return true
}
// * override the things *
override func drawRect(rect: CGRect) {
guard inner_circle != nil else {
print("can't draw yet because circle beziers have not been made")
return
}
let context = UIGraphicsGetCurrentContext()
CGContextClearRect(context, self.bounds)
circle_color.setStroke()
drawn_circle.stroke()
if touch_trail != nil { trail_color.setStroke(); touch_trail.stroke() }
if touch_circle != nil { touch_color.setFill(); touch_circle.stroke(); touch_circle.fill() }
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!.locationInView(self)
if outer_circle.containsPoint(touch) && inner_circle.containsPoint(touch) == false {
startTouch(atPoint: touch)
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!.locationInView(self)
if outer_circle.containsPoint(touch) && inner_circle.containsPoint(touch) == false {
moveTouch(toPoint: touch)
} else {
touchFailed()
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
// well, same thing as if the touchesMoved
let touch = touches.first!.locationInView(self)
if outer_circle.containsPoint(touch) && inner_circle.containsPoint(touch) == false {
moveTouch(toPoint: touch)
} else {
touchFailed()
}
}
// * where things happen, states get wild *
private func startTouch(atPoint point: CGPoint) {
endTouching()
let circlePoint = getPointOnCircle(forPoint: point)
touch_circle = UIBezierPath(arcCenter: circlePoint, radius: touch_diameter / 2, startAngle: 0, endAngle: CGFloat(M_PI * 2), clockwise: true)
start_point = circlePoint
let circleRad = atan2(point.y - circle_center.y, point.x - circle_center.x)
let rads = getRads(withOffset: 0.1, fromRad: circleRad)
left_check = pointOnCircle(forRad: rads.0, withRadius: circle_diameter / 2)
right_check = pointOnCircle(forRad: rads.1, withRadius: circle_diameter / 2)
updateTouchTrail(toPoint: circlePoint)
self.setNeedsDisplay()
}
private func moveTouch(toPoint point: CGPoint) {
let circlePoint = getPointOnCircle(forPoint: point)
touch_circle = UIBezierPath(arcCenter: circlePoint, radius: touch_diameter / 2, startAngle: 0, endAngle: CGFloat(M_PI * 2), clockwise: true)
updateTouchTrail(toPoint: circlePoint)
if circled {
if touch_trail.containsPoint(start_point!) { circleCompleted() }
}
self.setNeedsDisplay()
}
private func endTouching() {
start_point = nil
start_rad = nil
left_check = nil
right_check = nil
circled = false
clockwise = nil
touch_circle = nil
touch_trail = nil
self.setNeedsDisplay()
}
private func updateTouchTrail(toPoint point: CGPoint) {
let rad = atan2(point.y - circle_center.y, point.x - circle_center.x)
touchMoved(rad)
guard start_rad != nil else { start_rad = rad; return }
if clockwise == nil {
// figure out if it is or not clockwise
if start_rad! < 0 {
if rad < 0 {
if start_rad! < rad { clockwise = true }
else { clockwise = false }
} else {
if start_rad! > -(1.5) { clockwise = true }
else { clockwise = false }
}
} else {
if rad > 0 {
if start_rad! < rad { clockwise = true }
else { clockwise = false }
} else {
if start_rad! > 1.5 { clockwise = true }
else { clockwise = false }
}
}
}
touch_trail = UIBezierPath(arcCenter: circle_center, radius: (circle_diameter / 2),
startAngle: start_rad!, endAngle: rad, clockwise: clockwise!)
touch_trail.lineWidth = circle_width
if !circled {
// we must check if it is now circled
if clockwise! { if left_check != nil { if touch_trail.containsPoint(left_check!) { circled = true } } }
else { if right_check != nil { if touch_trail.containsPoint(right_check!) { circled = true } } }
}
}
// * events are happening omg *
private func circleCompleted() {
endTouching()
if dissappear_on_completion { self.removeFromSuperview() }
if completed_selector != nil && completed_target != nil { completed_target!.performSelector(completed_selector!) }
}
private func touchFailed() {
endTouching()
if failed_selector != nil && failed_target != nil { failed_target!.performSelector(failed_selector!) }
}
private func touchMoved(rad: CGFloat) {
if moved_target != nil && moved_selector != nil { moved_target!.performSelector(moved_selector!, withObject: (rad as AnyObject)) }
}
// * helpers to help things *
private func getPointOnCircle(forPoint point: CGPoint) -> CGPoint {
let touchPoint = CGPointMake(point.x - circle_center.x, point.y - circle_center.y)
let touchRad = atan2(touchPoint.y, touchPoint.x)
return pointOnCircle(forRad: touchRad, withRadius: circle_diameter / 2)
}
private func getCircleValue(hypotenus c: CGFloat, leg a: CGFloat) -> CGFloat {
return sqrt( (pow(c, 2)) - (pow(abs(a), 2)) )
}
private func getRads(withOffset offset: CGFloat, fromRad rad: CGFloat) -> (CGFloat, CGFloat) {
var leftRad: CGFloat?
var rightRad: CGFloat?
if rad <= 0 {
leftRad = rad - offset
if leftRad! < -(CGFloat(M_PI)) { leftRad! = CGFloat(M_PI) - (CGFloat(M_PI) + leftRad!) }
} else {
leftRad = rad - offset
}
if rad <= 0 {
rightRad = rad + offset
} else {
rightRad = rad + offset
if rightRad > CGFloat(M_PI) { rightRad = -(CGFloat(M_PI)) + (rightRad! - CGFloat(M_PI)) }
}
return (leftRad!, rightRad!)
}
private func pointOnCircle(forRad rad: CGFloat, withRadius radius: CGFloat) -> CGPoint {
let x = radius * cos(rad)
let y = radius * sin(rad)
return CGPointMake(x + circle_center.x, y + circle_center.y)
}
// peace
}