forked from refactoring-challenge/reversi-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiskView.swift
66 lines (56 loc) · 1.67 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
import UIKit
public class DiskView: UIView {
/// このビューが表示するディスクの色を決定します。
public var disk: Disk = .dark {
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 uiColor: UIColor {
switch self {
case .dark: return UIColor(named: "DarkColor")!
case .light: return UIColor(named: "LightColor")!
}
}
fileprivate var cgColor: CGColor {
uiColor.cgColor
}
fileprivate var name: String {
switch self {
case .dark: return "dark"
case .light: return "light"
}
}
fileprivate init(name: String) {
switch name {
case Disk.dark.name:
self = .dark
case Disk.light.name:
self = .light
default:
preconditionFailure("Illegal name: \(name)")
}
}
}