gpt4 book ai didi

ios - 使用 UIBezierPath 的圆角半径

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:12:57 25 4
gpt4 key购买 nike

我正在尝试使用“UIBezierPath”来圆化 View 的角。我只需要舍入右上角和左上角。

我使用了以下代码

 -(void)setMaskOnView:(UIView *)givenView
{
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:givenView.bounds byRoundingCorners: (UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = givenView.bounds;
maskLayer.path = maskPath.CGPath;
givenView.layer.mask = maskLayer;
}

enter image description here

但是我的右上角不圆。

我用过

UIRectCornerAllCorners

但它不圆我的右角

enter image description here

我遗漏了什么吗??

最佳答案

我建议采用不同的方法。加载带有圆顶角的图像并设置为 CALayer 的内容。将此图层设置为 View 图层的蒙版。在给定 View 的 layoutSubivews 或给定 View Controller 的 viewDidLayoutSubviews 中更新 mask 层的大小。

加载图像作为图层内容

CALayer *maskLayer = [[CALayer alloc] init];
UIImage *maskImage = [UIImage imageNamed:@"mask_image.png" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
maskLayer.contents = (__bridge id _Nullable)(maskImage.CGImage);

mainLayer.mask = maskLayer

[编辑] 在评论中回答您的问题

无论是使用 CAShapeLayer 还是图像作为 mask ,您都必须调整 mask 层的大小,使其与 mask 层具有相同的大小。如果我们正在谈论 UITableViewCell 创建您自己的派生单元格并在 layoutSubviews 中更新 mask 形状。下面是示例代码(MyTableCell 从 Storyboard加载):

@interface MyTableCell ()

@property (nonatomic, strong) CAShapeLayer *maskLayer;

@end

@implementation MyTableCell

- (void)awakeFromNib
{
self.maskLayer = [[CAShapeLayer alloc] init];
self.layer.mask = self.maskLayer;
}

- (void)layoutSubviews
{
[super layoutSubviews];
self.maskLayer.path = [self maskPath].CGPath;
}

- (UIBezierPath *)maskPath
{
return [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners: (UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(10.0, 10.0)];

}

@end

关于ios - 使用 UIBezierPath 的圆角半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34897584/

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