-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiskView.swift
67 lines (57 loc) · 1.72 KB
/
DiskView.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
import UIKit
import Logic
public class DiskView: UIView {
/// このビューが表示するディスクの色を決定します。
public var disk: Disk = .diskDark {
didSet { setNeedsDisplay() }
}
/// Interface Builder からディスクの色を設定するためのプロパティです。 `"dark"` か `"light"` の文字列を設定します。
@IBInspectable public var name: String {
get { disk.name }
set { disk = .init(name: newValue) }
}
override public init(frame: CGRect) {
super.init(frame: frame)
setUp()
}
required public init?(coder: NSCoder) {
super.init(coder: coder)
setUp()
}
private func setUp() {
backgroundColor = .clear
isUserInteractionEnabled = false
}
override public func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
context.setFillColor(disk.cgColor)
context.fillEllipse(in: bounds)
}
}
extension Disk {
fileprivate var name: String {
switch self {
case .diskDark: return "dark"
case .diskLight: return "light"
}
}
fileprivate var uiColor: UIColor {
switch self {
case .diskDark: return UIColor(named: "DarkColor")!
case .diskLight: return UIColor(named: "LightColor")!
}
}
fileprivate var cgColor: CGColor {
uiColor.cgColor
}
fileprivate init(name: String) {
switch name {
case Disk.diskDark.name:
self = .diskDark
case Disk.diskLight.name:
self = .diskLight
default:
preconditionFailure("Illegal name: \(name)")
}
}
}