-
Notifications
You must be signed in to change notification settings - Fork 185
/
LiquittableCircle.swift
75 lines (64 loc) · 1.97 KB
/
LiquittableCircle.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
//
// LiquittableCircle.swift
// LiquidLoading
//
// Created by Takuma Yoshida on 2015/08/17.
// Copyright (c) 2015年 yoavlt. All rights reserved.
//
import Foundation
import UIKit
class LiquittableCircle : UIView {
var points: [CGPoint] = []
var isGrow = false {
didSet {
grow(isGrow)
}
}
var radius: CGFloat {
didSet {
setup()
}
}
var color: UIColor = UIColor.redColor()
init(center: CGPoint, radius: CGFloat, color: UIColor) {
let frame = CGRect(x: center.x - radius, y: center.y - radius, width: 2 * radius, height: 2 * radius)
self.radius = radius
self.color = color
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func move(dt: CGPoint) {
let point = CGPoint(x: center.x + dt.x, y: center.y + dt.y)
self.center = point
}
private func setup() {
self.frame = CGRect(x: center.x - radius, y: center.y - radius, width: 2 * radius, height: 2 * radius)
let bezierPath = UIBezierPath(ovalInRect: CGRect(origin: CGPointZero, size: CGSize(width: radius * 2, height: radius * 2)))
draw(bezierPath)
}
func draw(path: UIBezierPath) {
self.layer.sublayers?.each { $0.removeFromSuperlayer() }
let layer = CAShapeLayer(layer: self.layer)
layer.lineWidth = 3.0
layer.fillColor = self.color.CGColor
layer.path = path.CGPath
self.layer.addSublayer(layer)
if isGrow {
grow(true)
}
}
func grow(isGrow: Bool) {
if isGrow {
grow(self.color, radius: self.radius, shininess: 1.6)
} else {
self.layer.shadowRadius = 0
self.layer.shadowOpacity = 0
}
}
func circlePoint(rad: CGFloat) -> CGPoint {
return CGMath.circlePoint(center, radius: radius, rad: rad)
}
}