How to create rounded corners with CAShapeLayer

后端 未结 2 1182
半阙折子戏
半阙折子戏 2021-02-14 06:21

Is there a way to add rounded corners to a CAShapeLayer? In my case I needed the shape layer to create a dashed border via lineDashPattern.

2条回答
  •  盖世英雄少女心
    2021-02-14 07:07

    The answer is simple. Create a bezier path with rounded corners.

    self.clipsToBounds = YES; 
    self.layer.cornerRadius = 10.0;
    
    self.border = [CAShapeLayer layer];
    self.border.fillColor = nil;
    self.border.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:10.0];
    self.border.frame = self.bounds;
    
    self.border.strokeColor = [UIColor purpleColor].CGColor;
    self.border.lineWidth = borderWidth * 2; // double desired width as half will be clipped
    self.border.lineDashPattern = @[@15];
    
    [self.layer addSublayer:self.border];
    

    Rounded corners with dashed line - correct

提交回复
热议问题