Use Bezier Path as Clipping Mask

后端 未结 2 707
耶瑟儿~
耶瑟儿~ 2020-12-14 02:12

I am wondering if it is possible to clip a view to a Bezier Path. What I mean is that I want to be able to see the view only in the region within the closed Bezier Path. The

2条回答
  •  醉话见心
    2020-12-14 02:32

    You can do this easily by setting your view's layer mask to a CAShapeLayer.

    UIBezierPath *myClippingPath = ...
    
    CAShapeLayer *mask = [CAShapeLayer layer];
    mask.path = myClippingPath.CGPath;
    
    myView.layer.mask = mask;
    

    You will need to add the QuartzCore framework to your target if you haven't already.


    In Swift ...

    let yourCarefullyDrawnPath = UIBezierPath( .. blah blah
    let maskForYourPath = CAShapeLayer()
    maskForYourPath.path = carefullyRoundedBox.CGPath
    layer.mask = maskForYourPath
    

提交回复
热议问题