1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UIViewを丸型に切り取る

Last updated at Posted at 2018-12-31

切り取る

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
    }
}


結果

Simulator Screen Shot - iPhone SE - 2018-12-31 at 16.52.44.png
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?