Use Bezier Path as Clipping Mask

后端 未结 2 700
耶瑟儿~
耶瑟儿~ 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
    
    0 讨论(0)
  • 2020-12-14 02:32

    Just an example of Rob's solution, there's a UIWebView sitting as a subview of a UIView called smoothView. smoothView uses bezierPathWithRoundedRect to make a rounded gray background (notice on right). That works fine.

    But if smoothView has only normal clip-subviews, you get this:

    enter image description here

    If you do what Rob says, you get the rounded corners in smoothView and all subviews ...

    enter image description here

    Great stuff.

    0 讨论(0)
提交回复
热议问题