Created
March 29, 2018 15:11
-
-
Save yoman07/a249f515592c50d65eacc3f24bf9e9b7 to your computer and use it in GitHub Desktop.
View with inside corners
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 CornerInsideView: UIView { | |
var corner: UIRectCorner? { | |
didSet { | |
setup() | |
} | |
} | |
var radius: CGFloat = 0 | |
private func setup() { | |
var degrees: CGFloat = 0 //the value in degrees | |
guard let corner = corner else { return } | |
switch corner { | |
case .topRight: | |
degrees = 270 | |
case .bottomRight: | |
degrees = 180 | |
case .bottomLeft: | |
degrees = 0 | |
case .topLeft: | |
degrees = 45 | |
default: | |
break | |
} | |
transform = CGAffineTransform(rotationAngle: degrees * CGFloat.pi / 2) | |
} | |
override func layoutSubviews() { | |
let mask = CAShapeLayer() | |
mask.frame = layer.bounds | |
let path = UIBezierPath() | |
let radius: CGFloat = self.radius | |
let rect = mask.bounds | |
path.move(to: CGPoint(x: rect.maxX, y: rect.maxY)) | |
path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY)) | |
path.addArc(withCenter: CGPoint(x: rect.minX + radius, | |
y: rect.maxY - radius), | |
radius: radius, | |
startAngle: CGFloat.pi, | |
endAngle: CGFloat.pi/2, | |
clockwise: false) | |
path.close() | |
mask.path = path.cgPath | |
layer.mask = mask | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment