Skip to content

Instantly share code, notes, and snippets.

@bricklife
Created September 20, 2017 14:12
Show Gist options
  • Save bricklife/dc1b60dcd2140446db7532701656dc42 to your computer and use it in GitHub Desktop.
Save bricklife/dc1b60dcd2140446db7532701656dc42 to your computer and use it in GitHub Desktop.
import UIKit
private func angle(_ degree: Double) -> CGFloat {
return CGFloat(Double.pi * degree / 180.0)
}
let bounds = CGRect(x: 0, y: 0, width: 150, height: 100)
// corner radius for bottom right, bottom left, top left, and top right
let r: (CGFloat, CGFloat, CGFloat, CGFloat) = (5, 10, 20, 40)
let path = UIBezierPath()
path.addArc(withCenter: CGPoint(x: bounds.maxX - r.0, y: bounds.maxY - r.0), radius: r.0, startAngle: angle(0), endAngle: angle(90), clockwise: true)
path.addArc(withCenter: CGPoint(x: bounds.minX + r.1, y: bounds.maxY - r.1), radius: r.1, startAngle: angle(90), endAngle: angle(180), clockwise: true)
path.addArc(withCenter: CGPoint(x: bounds.minX + r.2, y: bounds.minY + r.2), radius: r.2, startAngle: angle(180), endAngle: angle(270), clockwise: true)
path.addArc(withCenter: CGPoint(x: bounds.maxX - r.3, y: bounds.minY + r.3), radius: r.3, startAngle: angle(270), endAngle: angle(0), clockwise: true)
path.close()
let maskLayer = CAShapeLayer()
maskLayer.frame = bounds
maskLayer.path = path.cgPath
let view = UIView(frame: bounds)
view.layer.mask = maskLayer
view.backgroundColor = .red
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment