Friday, October 23, 2026
HomeiOS Developmentios - How do I masks and erase a CAShapeLayer?

ios – How do I masks and erase a CAShapeLayer?

[ad_1]

I’m attempting to get erase performance working with CAShapeLayer. Present code:

class MaskTestVC: UIViewController {

    override func viewDidLoad() {
        tremendous.viewDidLoad()

        view.backgroundColor = .white
        let picture = UIImage(named: "check.jpg")!

        let testLayer = CAShapeLayer()
        testLayer.contents = picture.cgImage
        testLayer.body = view.bounds

        let maskLayer = CAShapeLayer()
        maskLayer.opacity = 1.0
        maskLayer.lineWidth = 20.0
        maskLayer.strokeColor = UIColor.black.cgColor
        maskLayer.fillColor = UIColor.clear.cgColor
        maskLayer.body = view.bounds
        testLayer.masks = maskLayer

        view.layer.addSublayer(testLayer)
    }

}

I used to be considering if I create a masks layer then I might draw a path on the masks layer and erase components of the picture eg:

let path = UIBezierPath()
path.transfer(to: CGPoint(x: 100.0, y: 100.0))
path.addLine(to: CGPoint(x: 200.0, y: 200.0))
maskLayer.path = path.cgPath

Nevertheless plainly once I add the maskLayer it covers the whole picture and I can not see the contents under it. What am I doing fallacious right here?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments