【问题标题】:iOS shape with border and corner cutoff带有边框和切角的 iOS 形状
【发布时间】:2023-06-03 11:12:02
【问题描述】:

我有这个形状: http://screencast.com/t/9UUhAXT5Wu

但边界没有在截止时跟随它 - 我该如何修复它?

这是我当前视图的代码:

self.view.backgroundColor = color;
CALayer *backgroundLayer = self.view.layer;

CAShapeLayer *mask = CAShapeLayer.new;
mask.frame = backgroundLayer.bounds;
mask.fillColor = [[UIColor blackColor] CGColor];

CGFloat width = backgroundLayer.frame.size.width;
CGFloat height = backgroundLayer.frame.size.height;

CGMutablePathRef path = CGPathCreateMutable();

int cornerCutSize = 30;
CGPathMoveToPoint(path, NULL, 0, 0);
CGPathAddLineToPoint(path, nil, width, 0);
CGPathAddLineToPoint(path, nil, width, height - cornerCutSize);
CGPathAddLineToPoint(path, nil, width - cornerCutSize, height);
CGPathAddLineToPoint(path, nil, width, height);
CGPathAddLineToPoint(path, nil, 0, height);
CGPathAddLineToPoint(path, nil, 0, 0);
CGPathCloseSubpath(path);

mask.path = path;
CGPathRelease(path);

backgroundLayer.mask = mask;

//add border
CGColorRef borderColor = [UIColor redColor].CGColor;
[backgroundLayer setBorderColor:borderColor];
[backgroundLayer setBorderWidth:3];

【问题讨论】:

    标签: ios cgpath cashapelayer


    【解决方案1】:

    设置遮罩层不会改变边框的形状。您需要创建两层并将一层分配给蒙版并描边一层,如下所示:

    CAShapeLayer *mask = [CAShapeLayer new];
    mask.frame = backgroundLayer.bounds;
    mask.path = path;
    backgroundLayer.mask = mask;
    
    CAShapeLayer *stroke = [CAShapeLayer new];
    stroke.frame = backgroundLayer.bounds;
    stroke.path = path;
    stroke.lineWidth = 3;
    stroke.strokeColor = [UIColor redColor].CGColor;
    stroke.fillColor = nil;
    
    [backgroundLayer addSublayer:stroke];
    

    【讨论】:

    • ROFL,真的吗?您要编辑以将空间从星号之后移动到它之前?任何让你开心的东西。
    最近更新 更多