ios – 绕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的图层具有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];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...