Bezier and disguise

I want to configure UIBezierPath as a mask in my view: The goal is this: enter image description here

So, I draw a view with a frame:

CGFloat round = 80; UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width/2-105, 0, 210, 60+round)]; myView.backgroundColor = [UIColor redColor]; 

And then try adding BezierMask:

 UIBezierPath *aPath = [UIBezierPath bezierPath]; CGSize viewSize = CGSizeMake(myView.frame.size.width, myView.frame.size.height); //(210,80) CGPoint startPoint = CGPointMake(myView.frame.origin.x,myView.frame.origin.y); //(279,0) [aPath moveToPoint:startPoint]; //(279,0) [aPath addLineToPoint:CGPointMake(startPoint.x+viewSize.width,startPoint.y)]; //(489,0) [aPath addLineToPoint:CGPointMake(startPoint.x+viewSize.width,startPoint.y+viewSize.height-round)]; //(489,60) [aPath addQuadCurveToPoint:CGPointMake(startPoint.x,startPoint.y+viewSize.height-round) controlPoint:CGPointMake(startPoint.x+(viewSize.width/2),80)]; //(279,60) : (384,80) [aPath closePath]; CAShapeLayer *layer = [CAShapeLayer layer]; layer.frame = myView.bounds; layer.path = aPath.CGPath; myView.layer.mask = layer; [self addSubview:myView]; 

But after that my opinion disappears. Why?

+4
source share
1 answer

I assume that you are using the wrong coordinates for the mask layer. Your mask layer should be placed in the view that it is masking, and not in the viewview.

Try these changes:

 CGSize viewSize = myView.bounds.size; CGPoint startPoint = CGPointZero; 

Your code with these small changes works fine for me.

+4
source

Source: https://habr.com/ru/post/1482975/


All Articles