0
我正在使用核心圖形來創建正弦圖。我完成了繪製圖表的所有工作,並且工作得很好,因爲我只需要在兩點聯合處添加曲線。如何使用coreGraphics在兩點聯合處添加曲線?
我的圖表看起來像下面的圖片:
我要畫一條線像下面的圖片中,但我不知道如何:
有人可以幫我解決這個問題嗎?
我的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);
}
我想你需要在這裏使用貝塞爾路徑。 http://developer.apple.com/library/ios/#documentation/2ddrawing/conceptual/drawingprintingios/BezierPaths/BezierPaths.html – filwag 2013-04-25 14:29:02
請不要在'drawRect:'內添加子視圖。每當重新繪製視圖時,您都會一遍又一遍地添加它們。爲此,使用'drawInRect:'或'drawAtPoint:' – 2013-04-28 16:54:55