Created
June 17, 2019 15:21
-
-
Save NeilsUltimateLab/f7a8256c43e3fd7e210d0df3c455cdd9 to your computer and use it in GitHub Desktop.
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
class NotchView: UIView { | |
var notchWidth: CGFloat = 16 | |
var notchHeight: CGFloat = 12 | |
var cornerRadius: CGFloat = 8 | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
self.isUserInteractionEnabled = true | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
updateBorder() | |
} | |
func updateBorder() { | |
let bezierPath = UIBezierPath() | |
let bottomRightCenterPoint = CGPoint(x: self.bounds.width - cornerRadius, | |
y: self.bounds.height - (notchHeight) - cornerRadius) | |
let bottomLeftCenterPoint = CGPoint(x: cornerRadius, | |
y: self.bounds.height - (notchHeight) - cornerRadius) | |
let topLeftCenterPoint = CGPoint(x: cornerRadius, | |
y: cornerRadius) | |
let topRightCenterPoint = CGPoint(x: self.bounds.width - cornerRadius, | |
y: cornerRadius) | |
let notchRightPoint = CGPoint(x:self.bounds.width/2 + (notchWidth/2), | |
y: self.bounds.height - (notchHeight)) | |
let notchCenterPoint = CGPoint(x: self.bounds.width/2, | |
y: self.bounds.height) | |
let notchLeftPoint = CGPoint(x:self.bounds.width/2 - (notchWidth/2), | |
y: self.bounds.height - (notchHeight)) | |
// bottom right | |
bezierPath.addArc(withCenter: bottomRightCenterPoint, | |
radius: cornerRadius, | |
startAngle: 0, | |
endAngle: CGFloat.pi/2, | |
clockwise: true) | |
// notch | |
bezierPath.addLine(to: notchRightPoint) | |
bezierPath.addLine(to: notchCenterPoint) | |
bezierPath.addLine(to: notchLeftPoint) | |
// bottom left | |
bezierPath.addArc(withCenter: bottomLeftCenterPoint, | |
radius: cornerRadius, | |
startAngle: (CGFloat.pi / 2), | |
endAngle: CGFloat.pi, | |
clockwise: true) | |
// top left | |
bezierPath.addArc(withCenter: topLeftCenterPoint, | |
radius:cornerRadius, | |
startAngle: CGFloat.pi, | |
endAngle: (3 * CGFloat.pi / 2), | |
clockwise: true) | |
// top right | |
bezierPath.addArc(withCenter: topRightCenterPoint, | |
radius: cornerRadius, | |
startAngle: (3 * CGFloat.pi / 2), | |
endAngle: (2 * CGFloat.pi), | |
clockwise: true) | |
bezierPath.stroke() | |
let shapeLayer = CAShapeLayer() | |
shapeLayer.path = bezierPath.cgPath | |
self.layer.mask = shapeLayer | |
self.layer.cornerRadius = 8 | |
self.layer.masksToBounds = true | |
} | |
override func systemLayoutSizeFitting(_ targetSize: CGSize, | |
withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, | |
verticalFittingPriority: UILayoutPriority) -> CGSize { | |
let size = super.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: horizontalFittingPriority, verticalFittingPriority: verticalFittingPriority) | |
return CGSize(width: size.width, height: size.height + notchHeight + 2) | |
} | |
func configure(with place: Place) { | |
self.titleLabel.text = place.name | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment