UIBezierPath - 带圆角的矩形

时间:2017-09-18 10:03:53

标签: ios objective-c bar-chart uibezierpath rounded-corners

我正在使用dr-Charts API在我的应用中绘制条形图。我正在绘制条形图:

BarChart *barChartView = [[BarChart alloc] initWithFrame:CGRectMake(0, 150, WIDTH(self.view), HEIGHT(self.view) - 600)];
[barChartView setDataSource:self];
[barChartView setDelegate:self];
[barChartView setLegendViewType:LegendTypeHorizontal];
[barChartView setShowCustomMarkerView:TRUE];
[barChartView drawBarGraph];

[barChartView setDrawGridY:TRUE];
[barChartView setDrawGridX:FALSE];

[self.view addSubview:barChartView];

实际上,我希望我的条形图矩形为圆角矩形,类似于此:

enter image description here

所以,在 BarChart.m 中,我开始使用API​​ CAShapeLayer& UIBezierPath

- (UIBezierPath *)drawBarPathWithStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint{
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(startPoint.x, startPoint.y, endPoint.x - startPoint.x, endPoint.y - startPoint.y)];
  //UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(startPoint.x, startPoint.y, endPoint.x - startPoint.x, endPoint.y - startPoint.y) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10, 10)];
    [path stroke];

    return path;
}

        CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
        [shapeLayer setPath:[[self drawBarPathWithStartPoint:endPoint endPoint:startPoint] CGPath]];
        [shapeLayer setStrokeColor:[barData.barColor CGColor]];
        [shapeLayer setFillColor:[barData.barColor CGColor]];
        [shapeLayer setFillRule:kCAFillRuleEvenOdd];
        [shapeLayer setLineWidth:0.5f];
        [shapeLayer setOpacity:0.7f];
        [shapeLayer setShadowRadius:0.0f];
        [shapeLayer setShadowColor:[[UIColor clearColor] CGColor]];
        [shapeLayer setShadowOpacity:0.0f];
        [shapeLayer setValue:[barData.yAxisArray objectAtIndex:i] forKey:@"data"];

在StackOverflow上关注了很多答案后,我无法成功。 我尝试了许多答案,例如
shapeLayer.cornerRadius = 10;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(startPoint.x, startPoint.y, endPoint.x - startPoint.x, endPoint.y - startPoint.y) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10, 10)];

你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

尝试绘制带圆角的宽线,例如

UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:CGPointMake(topX, topY)];
[linePath addLineToPoint:CGPointMake(bottomX, bottomY)];

CAShapeLayer *line = [CAShapeLayer layer];
line.frame = some_frame;
line.lineCap = kCALineCapRound;
line.path = linePath.CGPath;
line.fillColor = nil;
line.lineWidth = 6.0;
line.cornerRadius = 3.0;
line.opacity = 1.0;
line.strokeColor = [UIColor greenColor].CGColor;
[line setMasksToBounds:YES];