Last active
May 4, 2017 17:26
-
-
Save trm36/23490e3f6b782f08f25eb5f18a7f556c to your computer and use it in GitHub Desktop.
Update Triangle View Color Dynamically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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