Skip to content

Instantly share code, notes, and snippets.

@trilliwon
Created March 10, 2020 02:00
Show Gist options
  • Save trilliwon/c5283248a84f2194e0ae788d9123d7b7 to your computer and use it in GitHub Desktop.
Save trilliwon/c5283248a84f2194e0ae788d9123d7b7 to your computer and use it in GitHub Desktop.
Clockfase
import UIKit
@IBDesignable
final class ClockFace: UIView {
var numberTextFont: UIFont = UIFont.boldSystemFont(ofSize: 11)
@IBInspectable var textColor: UIColor = .black
@IBInspectable var tickColor: UIColor = .black
@IBInspectable var fiveTickColor: UIColor = .black
@IBInspectable var centerDotColor: UIColor = .black
@IBInspectable var borderColor: UIColor = .clear
@IBInspectable var faceColor: UIColor = .clear
@IBInspectable var tickWidth: CGFloat = 1.0
@IBInspectable var fiveTickWidth: CGFloat = 4.0
@IBInspectable var isHiddenNumber: Bool = false
@IBInspectable var isHiddenCenterDot: Bool = true
private var radius: Double {
return Double(bounds.width / 2)
}
private var innerRadius: Double {
return Double(bounds.width / 2) - 30
}
override func draw(_ rect: CGRect) {
let drawRect = rect.insetBy(dx: 40, dy: 40)
// Border
borderColor.setStroke()
let facePath = UIBezierPath(ovalIn: drawRect)
facePath.stroke()
faceColor.setFill()
facePath.fill()
// Ticks
tickColor.set()
for tick in 1...60 {
let angle = Double(tick) * 6.0 * Double.pi / 180
let tickPath = UIBezierPath()
if tick % 5 == 0 {
let point = CGPoint(x: radius + cos(angle) * innerRadius,
y: radius + sin(angle) * innerRadius)
tickPath.lineWidth = fiveTickWidth
tickPath.move(to: point)
fiveTickColor.setStroke()
tickPath.addLine(to: CGPoint(x: radius + cos(angle) * (innerRadius - 10),
y: radius + sin(angle) * (innerRadius - 10)))
} else {
let point = CGPoint(x: radius + cos(angle) * (innerRadius - 5),
y: radius + sin(angle) * (innerRadius - 5))
tickPath.lineWidth = tickWidth
tickPath.move(to: point)
tickColor.setStroke()
tickPath.addLine(to: CGPoint(x: radius + cos(angle) * (innerRadius - 10),
y: radius + sin(angle) * (innerRadius - 10)))
}
tickPath.stroke()
}
if !isHiddenCenterDot {
// Center
let centerPoint = CGPoint(x: bounds.width / 2 - 5.0,
y: bounds.height / 2 - 5.0)
let centerRect = CGRect(origin: centerPoint, size: CGSize(width: 10, height: 10))
let centerPath = UIBezierPath(ovalIn: centerRect)
centerDotColor.setFill()
centerPath.fill()
}
// Draw Numbers
if !isHiddenNumber {
let textRadius = innerRadius + 15
textColor.set()
for number in 0...11 {
let numberText = NSAttributedString(string: String((number) * 5),
attributes:[.foregroundColor: textColor,
.font: UIFont.boldSystemFont(ofSize: 15)])
let angle = -(-(Double(number) * 30.0) + 90) * Double.pi / 180
let numberRect = CGRect(
x: CGFloat(radius + cos(angle) * textRadius - Double(numberText.size().width / 2)),
y: CGFloat(radius + sin(angle) * textRadius - Double(numberText.size().height / 2)),
width: numberText.size().width,
height: numberText.size().height)
numberText.draw(in: numberRect)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment