-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWatchKitActivityIndicator.swift
136 lines (103 loc) · 3.88 KB
/
WatchKitActivityIndicator.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
//
// WatchKitActivityIndicator.swift
// WatchKitActivityIndicator
//
// Created by Andy Drizen on 03/01/2015.
// Copyright (c) 2015 Andy Drizen. All rights reserved.
//
import UIKit
class WatchKitActivityIndicator: UIView {
let numberOfBalls = 6
var shouldStop = false
var offset: CGFloat = 0
var speed: CGFloat = 0
var displayLink : CADisplayLink?
var alphas : [CGFloat] = []
var scales : [CGFloat] = []
var stoppedCompletionHandler: (() -> Void)?
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clearColor()
resetProperties()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func resetProperties() {
if (displayLink != nil) {
displayLink!.invalidate()
}
speed = 3
offset = 0
alphas = []
scales = []
shouldStop = false
for i in 0..<numberOfBalls {
alphas.append(0.2 - CGFloat(numberOfBalls - i - 1) * 0.3)
scales.append(0.2 - CGFloat(numberOfBalls - i - 1) * 0.3)
}
}
func startAnimating() {
resetProperties()
displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:")
displayLink!.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes)
}
func stopAnimating(completion: () -> Void) {
shouldStop = true
stoppedCompletionHandler = completion
}
override func drawRect(rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
CGContextClearRect(context, rect)
let center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect))
var referenceBallWidth = floor(CGRectGetWidth(rect) / (CGFloat(numberOfBalls)/2)) - 1
let radius = Double(CGRectGetWidth(rect) - referenceBallWidth) / 2
for i in 0..<numberOfBalls {
let angle = -1 * M_PI/2 + M_PI/180 * (Double(CGFloat(i) * 360.0 / CGFloat(numberOfBalls)) + Double(offset))
let x = center.x + CGFloat(sin(angle) * radius)
let y = center.y + CGFloat(cos(angle) * radius)
let ballWidth = referenceBallWidth * max(0, scales[i])
UIColor(white: 1.0, alpha:max(0, min(alphas[i], 1))).set()
CGContextAddEllipseInRect(context, CGRectMake(x - ballWidth/2, y - ballWidth/2, ballWidth, ballWidth))
CGContextDrawPath(context, kCGPathFill)
}
}
func handleDisplayLink(displayLink : CADisplayLink) {
let growthRate = CGFloat(0.08)
let maxSpeed : CGFloat = 5
if (speed < maxSpeed) {
speed = speed + 2 * growthRate
let scale = speed/maxSpeed
transform = CGAffineTransformMakeScale(scale, scale)
}
if (speed > maxSpeed) {
speed = maxSpeed
transform = CGAffineTransformIdentity
}
offset = offset - speed
if (shouldStop) {
for i in 0..<numberOfBalls {
alphas[i] = max(0, alphas[i] - growthRate)
}
if (maxElement(alphas) <= 0) {
if (stoppedCompletionHandler != nil) {
stoppedCompletionHandler!()
}
displayLink.invalidate()
}
}
else{
if (minElement(alphas) <= 1) {
for i in 0..<numberOfBalls {
alphas[i] = min(1, alphas[i] + growthRate)
}
}
if (minElement(scales) <= 1) {
for i in 0..<numberOfBalls {
scales[i] = min(1, scales[i] + growthRate)
}
}
}
setNeedsDisplay()
}
}