[英]how to set cornerRadius of mask layer
I made a view. 我提出了一个观点。 Then I added a mask layer like this.
然后我添加了这样的遮罩层。
UIView *dd = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
dd.backgroundColor = [UIColor redColor];
dd.layer.masksToBounds = YES;
dd.layer.cornerRadius = 30.0f;
[self.view addSubview:dd];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGRect maskRect = CGRectMake(100, 100, 200, 200);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, nil, maskRect);
[maskLayer setPath:path];
CGPathRelease(path);
dd.layer.mask = maskLayer;
and how can I cornerRadius on maskLayer? 我怎么能在maskLayer上使用cornerRadius?
I tried to 我试过了
dd.layer.masksToBounds = YES;
dd.layer.cornerRadius = 30.0f;
it doesn't work. 它不起作用。 and other things too.
和其他事情一样。
Any way may be you want like this : 你可能想要这样的方式:
Add QuartzCore Framework in your Project 在项目中添加QuartzCore Framework
// Import in .m file //导入.m文件
#import <QuartzCore/QuartzCore.h>
// Do this code //做这个代码
UIView *dd = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
dd.backgroundColor = [UIColor redColor];
dd.layer.masksToBounds = YES;
dd.layer.borderColor = [UIColor whiteColor].CGColor;
[self.view addSubview:dd];
CGFloat cornerRadius = 0;
CGFloat borderWidth = 2;
UIColor *lineColor = [UIColor orangeColor];
CGRect maskRect = CGRectMake(100, 100, 200, 200);
//drawing
CGRect frame =maskRect;
CAShapeLayer *_shapeLayer = [CAShapeLayer layer];
//creating a path
CGMutablePathRef path = CGPathCreateMutable();
//drawing a border around a view
CGPathMoveToPoint(path, NULL, 0, frame.size.height - cornerRadius);
CGPathAddLineToPoint(path, NULL, 0, cornerRadius);
CGPathAddArc(path, NULL, cornerRadius, cornerRadius, cornerRadius, M_PI, -M_PI_2, NO);
CGPathAddLineToPoint(path, NULL, frame.size.width - cornerRadius, 0);
CGPathAddArc(path, NULL, frame.size.width - cornerRadius, cornerRadius, cornerRadius, -M_PI_2, 0, NO);
CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height - cornerRadius);
CGPathAddArc(path, NULL, frame.size.width - cornerRadius, frame.size.height - cornerRadius, cornerRadius, 0, M_PI_2, NO);
CGPathAddLineToPoint(path, NULL, cornerRadius, frame.size.height);
CGPathAddArc(path, NULL, cornerRadius, frame.size.height - cornerRadius, cornerRadius, M_PI_2, M_PI, NO);
//path is set as the _shapeLayer object's path
_shapeLayer.path = path;
CGPathRelease(path);
_shapeLayer.backgroundColor = [[UIColor clearColor] CGColor];
_shapeLayer.frame = frame;
_shapeLayer.masksToBounds = NO;
_shapeLayer.fillColor = [[UIColor grayColor] CGColor];
_shapeLayer.strokeColor = [lineColor CGColor];
_shapeLayer.lineWidth = borderWidth;
//_shapeLayer is added as a sublayer of the view, the border is visible
[dd.layer addSublayer:_shapeLayer];
dd.layer.cornerRadius = cornerRadius;
May be it will work. 可能会起作用。
happy coding. 快乐的编码。
You are making a layer from the path as I see. 你正在我看到的路径中创建一个图层。 Therefore, rounder corners should be already in the path to take any effect.
因此,圆角应该已经在路径中起作用。 Try using not just rect but the Bezier curves.
尝试不仅使用rect而且使用Bezier曲线。 Create a
UIBezierPath
with cornered radius from your rect like this: 从你的rect创建一个带有角度半径的
UIBezierPath
,如下所示:
+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;
and use it for your layer's path 并将其用于图层的路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.