-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDartBoard.swift
62 lines (58 loc) · 2.39 KB
/
DartBoard.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
import UIKit
@IBDesignable
class DartsView: UIView {
@IBInspectable var circles: UInt = 6 { didSet{ setNeedsDisplay() }}
@IBInspectable var scale: CGFloat = 0.9 { didSet{ setNeedsDisplay() }}
@IBInspectable var backColor: UIColor = UIColor.black { didSet{ setNeedsDisplay() }}
@IBInspectable var radiusDifference: UInt = 22 { didSet{ setNeedsDisplay() }}
@IBInspectable var lineWidth: CGFloat = 3.0 { didSet{ setNeedsDisplay() }}
@IBInspectable var lineColor: UIColor = UIColor.darkGray { didSet{ setNeedsDisplay() }}
@IBInspectable var bullsEye: UIColor = UIColor.black { didSet{ setNeedsDisplay() }}
var canDrawPoint: Bool = false
@IBInspectable var pointSize: CGFloat = 3.0 { didSet{ setNeedsDisplay() }}
@IBInspectable var pointColor: UIColor = UIColor.red { didSet{ setNeedsDisplay() }}
var point: CGPoint = CGPoint(x: 0.0, y: 0.0) { didSet{ setNeedsDisplay() }}
private var dartOuterRadius: CGFloat{
return min(bounds.width, bounds.height) / 2 * scale
}
private var dartCenter: CGPoint{
return convert(center, from: superview)
}
private func setProperties(forObject object: inout UIBezierPath){
lineColor.setStroke()
object.lineWidth = lineWidth
}
private func radiusOfCircleWithNumber(_ number: UInt) -> CGFloat{
return CGFloat(dartOuterRadius - CGFloat(radiusDifference * number))
}
func drawPoint(){
let dartPoint = drawCircleOf(radius: pointSize, center: point)
dartPoint.move(to: point)
pointColor.set()
dartPoint.stroke()
dartPoint.fill()
}
private func drawCircleOf(radius: CGFloat, center: CGPoint) -> UIBezierPath{
return UIBezierPath(arcCenter: center, radius: radius, startAngle: 0, endAngle: 2 * CGFloat(Double.pi), clockwise: true)
}
private func drawDart(){
for i in 0 ..< circles{
let radius = radiusOfCircleWithNumber(i)
guard radius > 0 else{
return
}
var object = drawCircleOf(radius: radius, center: dartCenter)
setProperties(forObject: &object)
i == circles - 1 ? bullsEye.setFill() : backColor.setFill()
object.fill()
object.stroke()
}
}
override func draw(_ rect: CGRect) {
// Drawing code
drawDart()
if(canDrawPoint){
drawPoint()
}
}
}