Created
August 22, 2019 22:33
-
-
Save yunustek/e696d8367656df5090b91bc23cde182d to your computer and use it in GitHub Desktop.
Trapezoid View
This file contains 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
final class TrapezoidView: UIView { | |
@IBInspectable | |
var isLeft: Bool = true { | |
didSet{ | |
self.initialize() | |
} | |
} | |
@IBInspectable | |
var color: UIColor = .white { | |
didSet{ | |
self.initialize() | |
} | |
} | |
private var path = UIBezierPath() | |
override func draw(_ rect: CGRect) { | |
self.initialize() | |
} | |
func initialize() { | |
self.alpha = 1 | |
self.backgroundColor = .clear | |
self.path = UIBezierPath() | |
self.path.move(to: CGPoint(x: 0, y: 0)) | |
if self.isLeft { | |
self.leftBottom() | |
} else { | |
self.rightBottom() | |
} | |
self.path.close() | |
self.color.setFill() | |
self.path.fill() | |
UIColor.clear.setStroke() | |
self.path.stroke() | |
} | |
func leftBottom() { | |
self.path.addLine(to: CGPoint(x: self.frame.width, y: 0)) | |
self.path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height)) | |
self.path.addLine(to: CGPoint(x: 15, y: self.frame.height)) | |
} | |
func rightBottom() { | |
self.path.addLine(to: CGPoint(x: self.frame.width, y: 0)) | |
self.path.addLine(to: CGPoint(x: self.frame.width - 15, y: self.frame.height)) | |
self.path.addLine(to: CGPoint(x: 0, y: self.frame.height)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment