class ViewController: UIViewController {
@IBOutlet weak var baseView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// 切り取りたい CGRectを指定
let rect = CGRect(x: 120, y: 10, width: 100, height: 100)
baseView.cutOutCircle(rect: rect)
}
}
extension UIView {
public final func cutOutCircle(rect: CGRect) {
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
let path = UIBezierPath(rect: self.bounds)
path.append(UIBezierPath(ovalIn: rect))
maskLayer.fillRule = .evenOdd
maskLayer.path = path.cgPath
self.layer.mask = maskLayer
}
}