围绕UIView的一些角落并围绕视图的图层边界

我试图绕过UIView的底部两个角,并使图层的边框也显示为圆形。 我目前在做:

UIRectCorners corners = UIRectCornerBottomLeft | UIRectCornerBottomRight; CGSize radii = CGSizeMake(kThisViewCornerRadius, kThisViewCornerRadius); UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:myView.bounds byRoundingCorners:corners cornerRadii:radii]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; [maskLayer setPath:path.CGPath]; myView.layer.mask = maskLayer; 

这适用于普通视图。 但是, myView的图层设置了borderColorborderWidth ,正如您从屏幕截图中看到的那样,图层的边框没有被舍入:

问题的屏幕截图

我还尝试了子类化UIView并从+[Class layerClass]返回[CAShapeLayer layer] ,并将视图的图层设置形状图层,但边框最终位于视图的子视图下方。

是否可以围绕视图的某个角落,围绕视图的图层边框,并将图层剪切到图层边框下方?

请注意,这不是关于如何围绕某些角落而不是其他角落,而是如何使笔划正确行为。

由于DavidRönnqvist的评论,我想出了一种新的思考方式。

我试图在一层中完成角落和笔划。 相反,我把它分成两层:一层用于遮盖视图的图层以使角落圆角,另一层用于添加笔划。

 UIView *containerView = [[UIView alloc] initWithFrame:someFrame]; UIRectCorners corners = UIRectCornerBottomLeft | UIRectCornerBottomRight; CGSize radii = CGSizeMake(kThisViewCornerRadius, kThisViewCornerRadius); UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:myView.bounds byRoundingCorners:corners cornerRadii:radii]; // Mask the container view's layer to round the corners. CAShapeLayer *cornerMaskLayer = [CAShapeLayer layer]; [cornerMaskLayer setPath:path.CGPath]; containerView.layer.mask = cornerMaskLayer; // Make a transparent, stroked layer which will dispay the stroke. CAShapeLayer *strokeLayer = [CAShapeLayer layer]; strokeLayer.path = path.CGPath; strokeLayer.fillColor = [UIColor clearColor].CGColor; strokeLayer.strokeColor = [UIColor redColor].CGColor; strokeLayer.lineWidth = 2; // the stroke splits the width evenly inside and outside, // but the outside part will be clipped by the containerView's mask. // Transparent view that will contain the stroke layer UIView *strokeView = [[UIView alloc] initWithFrame:containerView.bounds]; strokeView.userInteractionEnabled = NO; // in case your container view contains controls [strokeView.layer addSublayer:strokeLayer]; // configure and add any subviews to the container view // stroke view goes in last, above all the subviews [containerView addSubview:strokeView]; 

Zev Eisenberg的答案是正确的。

虽然我更喜欢:

 [self.layer addSublayer:strokeLayer]; 

而不是创建和添加子视图:

 CGSize radii = CGSizeMake(radius, radius); UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:radii]; // Mask the container view's layer to round the corners. CAShapeLayer *cornerMaskLayer = [CAShapeLayer layer]; [cornerMaskLayer setPath:path.CGPath]; self.layer.mask = cornerMaskLayer; // Make a transparent, stroked layer which will dispay the stroke. CAShapeLayer *strokeLayer = [CAShapeLayer layer]; strokeLayer.path = path.CGPath; strokeLayer.fillColor = [UIColor clearColor].CGColor; strokeLayer.strokeColor = color.CGColor; strokeLayer.lineWidth = 2; // the stroke splits the width evenly inside and outside, // but the outside part will be clipped by the containerView's mask. [self.layer addSublayer:strokeLayer]; 

这是小代码。 Alloc初始化视图并发送到此方法以获得四舍五入。 您可以选择围绕您想要的任何角落。 还给出阴影笔触颜色。

  -(void) setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners withColor: (UIColor*) color { UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(9.0, 9.0)]; CAShapeLayer* shape = [[[CAShapeLayer alloc] init] autorelease]; [shape setPath:rounded.CGPath]; shape.strokeColor = [[UIColor grayColor] CGColor]; view.backgroundColor=color; view.layer.mask = shape; } 

像这样调用方法。

 [self setMaskTo:ABCView byRoundingCorners:UIRectCornerAllCorners withColor:[UIColor greenColor]];