我试图绕过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的图层具有borderColor和borderWidth设置,从屏幕截图可以看出,图层的边框没有变圆:
我还尝试将UIView子类化并从[Class layerClass]返回[CAShapeLayer层],并将视图的图层设置为形状图层,但边框最终位于视图的子视图的下方.
可以围绕视图的边框,绕过视图层的边框,并剪辑层的边框下方的子视图?
请注意,这不是关于如何舍弃一些角落而不是其他角落,而是如何让笔触行为正确.
解决方法
我想出了一种新的思维方式,感谢
David Rö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];