Skip to content

Instantly share code, notes, and snippets.

@trm36
Last active May 4, 2017 17:26
Show Gist options
  • Save trm36/23490e3f6b782f08f25eb5f18a7f556c to your computer and use it in GitHub Desktop.
Save trm36/23490e3f6b782f08f25eb5f18a7f556c to your computer and use it in GitHub Desktop.
Update Triangle View Color Dynamically
//Updated for Swift 3.1
class TriangleView : UIView {
var color: UIColor = .black {
didSet {
setNeedsDisplay()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
context.beginPath()
context.move(to: CGPoint(x: rect.minX, y: rect.maxY))
context.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
context.addLine(to: CGPoint(x: (rect.maxX / 2.0), y: rect.minY))
context.closePath()
context.setFillColor(color.cgColor)
context.fillPath()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment