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

Zev*_*erg 15 objective-c calayer ios

我试图围绕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;
Run Code Online (Sandbox Code Playgroud)

这适用于普通视图.然而,myView的一层都有borderColorborderWidth集,因为你从截图中可以看到,该层的边框是没有得到四舍五入:

问题的屏幕截图

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

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

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

Zev*_*erg 36

感谢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];
Run Code Online (Sandbox Code Playgroud)


And*_*scu 10

Zev Eisenberg的答案是正确的.

虽然我更喜欢:

[self.layer addSublayer:strokeLayer];
Run Code Online (Sandbox Code Playgroud)

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

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];
Run Code Online (Sandbox Code Playgroud)