1

在此处输入图像描述

我做了一个看法。然后我添加了一个这样的遮罩层。

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;

以及如何在 maskLayer 上转角半径?

我尝试过了

dd.layer.masksToBounds = YES;
dd.layer.cornerRadius = 30.0f;

它不起作用。和其他事情。

4

2 回答 2

3

任何方式都可能是你想要的:

在此处输入图像描述

在您的项目中添加 QuartzCore 框架

// 导入.m文件

  #import <QuartzCore/QuartzCore.h>

// 执行此代码

 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;

也许它会起作用。

快乐编码。

于 2013-10-22T09:40:16.067 回答
1

如我所见,您正在从路径中创建一个图层。因此,圆角应该已经在路径中才能生效。尝试不仅使用矩形,还使用贝塞尔曲线。UIBezierPath从你的矩形创建一个带角半径的,如下所示:

+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;

并将其用于您的图层路径

于 2013-10-22T11:14:30.197 回答