Search code examples
iosobjective-cuibezierpathcashapelayercornerradius

How to set corner radius value with CAShaperLayer?


i drew a vertical line with UIBeizerPath and i need this line to have a corner radius with a value of 5. i tried to recall [pathLayer setCornerRadius: 5]; but I do not get results ... Can you help me? how can I assign a corner radius value? this is the code I use

    // crea le barre del grafico e gli assegna l'altezza della label y corrispondente
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(xPaddingFromYAxis +spaceBetweenBar *j, 200)];
    [path addLineToPoint:CGPointMake(xPaddingFromYAxis +spaceBetweenBar *j,  yLabelValue.center.y )];

    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.frame = self.bounds;
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [UIColor darkGrayColor].CGColor;
    pathLayer.fillColor = nil;
    pathLayer.lineWidth = 50;
    [pathLayer setCornerRadius:5];
    pathLayer.masksToBounds = NO;
    [scroll.layer addSublayer:pathLayer];

enter image description here


Solution

  • You need to set mask to bound as 'YES'. pathLayer.masksToBounds = YES;


    Try this and see:

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(xPaddingFromYAxis +spaceBetweenBar *j, 200)];
    [path addLineToPoint:CGPointMake(xPaddingFromYAxis +spaceBetweenBar *j,  yLabelValue.center.y )];
    
    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.frame = self.bounds;
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [UIColor darkGrayColor].CGColor;
    pathLayer.fillColor = nil;
    pathLayer.lineWidth = 50;
    [pathLayer setCornerRadius:5];
    pathLayer.masksToBounds = YES;
    [scroll.layer addSublayer:pathLayer];