gpt4 book ai didi

ios - 如何只为 UIView 的左下角、右下角和左上角设置 cornerRadius?

转载 作者:IT王子 更新时间:2023-10-29 07:46:06 24 4
gpt4 key购买 nike

有没有办法只为 UIView 的左下角、右下角和左上角设置 cornerRadius?

我尝试了以下方法,但最终使 View 消失了。下面的代码有什么问题吗?

    UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(20.0, 20.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.path = maskPath.CGPath;
view.layer.mask = maskLayer;

最佳答案

你可以这样做:

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.viewOutlet.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
self.viewOutlet.layer.mask = maskLayer;

enter image description here

更新:
如果您需要边框,只需创建另一个 CAShapeLayer 并将其添加到 View 的图层作为子图层。像这样(把这段代码放在上面的代码下面):

CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init];
borderLayer.frame = self.view.bounds;
borderLayer.path = maskPath.CGPath;
borderLayer.lineWidth = 4.0f;
borderLayer.strokeColor = [UIColor blackColor].CGColor;
borderLayer.fillColor = [UIColor clearColor].CGColor;

[self.viewOutlet.layer addSublayer:borderLayer];

enter image description here

在 swift 3.0 中是这样的:

let maskPath = UIBezierPath.init(roundedRect: self.viewOutlet.bounds, byRoundingCorners:[.topLeft, .bottomLeft], cornerRadii: CGSize.init(width: 10.0, height: 10.0))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.viewOutlet.bounds
maskLayer.path = maskPath.cgPath
self.viewOutlet.layer.mask = maskLayer

关于ios - 如何只为 UIView 的左下角、右下角和左上角设置 cornerRadius?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25616382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com