我一直在使用以下代码创建带圆角的视图:
- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];
CAShapeLayer* shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
view.layer.mask = shape;
}
所以我一直用以下的方式来称呼它:
[self setMaskTo:someView byRoundingCorners:UIRectCornerAllCorners];
或者:
[self setMaskTo:someOtherView byRoundingCorners:UIRectCornerBottomRight];
我现在想创建一些与在一个圆角外面的位之一相对应的视图 ......
一个好的方法(想想我)是创建适当的路径,这样我就可以创建一个CAShapeLayer
,然后将其用作掩码观点(类似于上面)。
在使用UIBezierPath
类(或CGPath
)时,我不确定最佳方法是什么,使用哪种方法等。
有什么建议吗?
答案 0 :(得分:4)
使用UIView
和掩码图层来获取我想要的形状,我只需要使用创建路径电弧 ...
我知道使用CGPath
的方法可以使用,但我已经使用UIBezierPath提供的方法。
我使用以下方法创建了我需要的路径:
- (UIBezierPath*)oppositeArcOfPathForBottomRightCorner
{
float width = _cornerRadius;
UIBezierPath* path = [UIBezierPath new];
[path moveToPoint:CGPointMake(0, width)];
[path addLineToPoint:CGPointMake(width, width)];
[path addLineToPoint:CGPointMake(width, 0)];
[path addArcWithCenter:CGPointMake(0, 0) radius:width startAngle:0 endAngle:(M_PI / 2.0f) clockwise:YES];
[path closePath];
return path;
}
- (UIBezierPath*)oppositeArcOfPathForTopLeftCorner
{
float width = _cornerRadius;
UIBezierPath* path = [UIBezierPath new];
[path moveToPoint:CGPointMake(0, width)];
[path addArcWithCenter:CGPointMake(width, width) radius:width startAngle:M_PI endAngle:(M_PI * 1.5f) clockwise:YES];
[path addLineToPoint:CGPointMake(0, 0)];
[path closePath];
return path;
}
然后像这样用作UIView
的掩码:
- (void)setMaskTo:(UIView*)view fromPath:(UIBezierPath*)path
{
CAShapeLayer* shape = [[CAShapeLayer alloc] init];
[shape setPath:path.CGPath];
view.layer.mask = shape;
}