gpt4 book ai didi

iphone - 如何使用 coreGraphics 在两点连接处添加曲线?

转载 作者:行者123 更新时间:2023-11-28 22:35:21 25 4
gpt4 key购买 nike

我正在使用核心图形创建正弦图。我做了所有关于绘制图表的事情,并且工作得很好,因为我只需要在两点连接处添加曲线。

我的图表如下图所示:

Example chart

我需要像下图那样画一条线,但我不知道怎么画:

Chart I want to generate

谁能帮我解决这个问题?

我的 DrawRect 代码如下

- (void)drawRect:(CGRect)rect
{
[self setClearsContextBeforeDrawing: YES];

CGContextRef context = UIGraphicsGetCurrentContext();


CGColorRef backColorRef = [UIColor blackColor].CGColor;
CGFloat backLineWidth = 2.f;
CGFloat backMiterLimit = 0.f;

CGContextSetLineWidth(context, backLineWidth);
CGContextSetMiterLimit(context, backMiterLimit);

CGContextSetShadowWithColor(context, CGSizeMake(3, 5), 8, backColorRef);

CGContextSetLineJoin(context, kCGLineJoinRound);

CGContextSetLineCap(context, kCGLineCapRound );

CGContextSetBlendMode(context, kCGBlendModeNormal);

CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);



int x = 320 ;
int y = 180 ;

for (int i=0; i<vDesc.count; i++)
{

CGPoint bPoint = CGPointMake(30, y);
CGPoint ePoint = CGPointMake(x, y);

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
[label setCenter:CGPointMake(bPoint.x-15, bPoint.y-30)];
[label setTextAlignment:UITextAlignmentCenter];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:[vDesc objectAtIndex:i]];
[self addSubview:label];

CGContextMoveToPoint(context, bPoint.x, bPoint.y-30);
CGContextAddLineToPoint(context, ePoint.x, ePoint.y-30);

y -= 30;

}
NSLog(@"%d",hDesc.count);
for (int i=0; i<hDesc.count; i++)
{

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(i*vInterval+5, 150, 30, 30)];
[label setTextAlignment:UITextAlignmentCenter];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
label.numberOfLines = 1;
label.adjustsFontSizeToFitWidth = YES;
label.minimumFontSize = 1.0f;
[label setText:[hDesc objectAtIndex:i]];

[self addSubview:label];
}



CGColorRef pointColorRef = [UIColor colorWithRed:24.0f/255.0f green:116.0f/255.0f blue:205.0f/255.0f alpha:1.0].CGColor;
CGFloat pointLineWidth = 1.5f;
// CGFloat pointMiterLimit = 5.0f;

CGContextSetLineWidth(context, pointLineWidth);
// CGContextSetMiterLimit(context, pointMiterLimit);


CGContextSetShadowWithColor(context, CGSizeMake(3, 5), 8, pointColorRef);

CGContextSetLineJoin(context, kCGLineJoinRound);

CGContextSetLineCap(context, kCGLineCapRound );

CGContextSetBlendMode(context, kCGBlendModeNormal);

CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);

if (array1.count!=0)
{
CGPoint p1 = [[array1 objectAtIndex:0] CGPointValue];
//int i = 1;
CGContextMoveToPoint(context, p1.x, 380-p1.y);

// CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400);
for (int i=0; i<[array1 count]; i++)
{
p1 = [[array1 objectAtIndex:i] CGPointValue];
CGPoint goPoint = CGPointMake(p1.x, 430-p1.y*vInterval/20);


CGContextAddLineToPoint(context, goPoint.x, goPoint.y);
UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];

[bt setBackgroundColor:[UIColor redColor]];

[bt setFrame:CGRectMake(0,0,3,3)];
[bt setCenter:goPoint];
[self addSubview:bt];
}

}
else
{
NSLog(@"empty");
}
CGContextStrokePath(context);

}

最佳答案

Bezier 路径将是很好的开始,您可以从 here 下载示例代码,对于曲线,你可以使用 [path addQuadCurveToPoint:nextPoint controlPoint:curvePoint];方法,对于此方法,您可以阅读 here

关于iphone - 如何使用 coreGraphics 在两点连接处添加曲线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16217205/

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