gpt4 book ai didi

ios - 仅在圆圈内用核心图形绘制

转载 作者:行者123 更新时间:2023-11-29 02:03:02 25 4
gpt4 key购买 nike

我需要实现一个 XY 动态绘图 - 它是实时绘制的。我有两种方法,首先我用填充颜色绘制圆圈:

- (void) drawCircleWithFillColorComponent:(float)value {
CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
CGFloat radius = MIN(center.x, center.y)-5.f;
UIBezierPath *path = [self bezierArcWithCenter:center
startAngle:0
endAngle:M_PI*2
radius:radius];
[[UIColor whiteAppColor] setStroke];
[[self colorWithValue:currentValue] setFill];
[path setLineWidth:1.f];
[path fill];
[path stroke];
}

然后我画了一张图:

- (void)drawHistoryInContext:(CGContextRef)context bounds:(CGRect)bounds {
CGFloat value;

UIBezierPath *graph = [UIBezierPath new];

for (NSUInteger counter = 0; counter < historyArray.count; counter++) {
value = [historyArray[counter] floatValue];
if (counter == 0) {
[graph moveToPoint:CGPointMake(bounds.origin.x+5+bounds.size.width/2/50, bounds.origin.y+bounds.size.height/2-(value-1)*bounds.size.height/12)];
} else {
[graph addLineToPoint:CGPointMake(bounds.origin.x+5+(float)counter/(float)(50-1)*bounds.size.width/2, MAX(bounds.origin.y+bounds.size.height/2-(value-1)*bounds.size.height/12,0))];
}
}

[graph setLineWidth:2.f];
[[UIColor lightBlueAppColor] setStroke];
[graph stroke];
}

问题是——图表的背景是一个圆圈。所以我需要将图形剪到圆圈内。现在绘制图形的区域是一个矩形——有时图形线显示在圆圈边界之外。如何裁剪图表?

不需要的行为是这样的:

enter image description here

最佳答案

您可以裁剪上下文,具体取决于您的 drawRect: 实现。本质上,在绘制图形线之前先进行剪辑,然后再绘制。裁剪会影响之后发生的所有绘图,您还可以保存上下文(使用 CGContextSaveGState)然后恢复(使用 CGContextRestoreGState)如果您需要稍后绘制任何不想被剪辑。

关于ios - 仅在圆圈内用核心图形绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30119932/

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