0

我被困在这个问题上。我需要在上一个终点和下一个起点之间画一条线。我在两点之间画线的代码是

- (void)drawRect:(CGRect)rect
{
    CGContextRef c=UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(c, 4.0);
    CGFloat red[4]={0.0f, 0.0f, 0.0f, 1.0f};
    CGContextSetStrokeColor(c, red);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 50.0f, 50.0f);
    CGContextAddLineToPoint(c, 100.0f, 100.0f);
    CGContextStrokePath(c) ;
}

我怎样才能给出多个点,以便我可以像图表一样显示。提前致谢

4

3 回答 3

3

顺便说一句,Apple 更喜欢您使用更高的 UIKit 方法而不是 Core Graphics 调用来绘制线条和曲线等简单的东西,因此您可以像这样重写您的方法:

[[UIColor redColor] setStroke];

UIBezierPath *linePath = [UIBezierPath bezierPath];

[linePath moveToPoint:CGPointMake(50.0, 50.0)];
[linePath addLineToPoint:CGPointMake(100.0, 100.0)];
[linePath setLineWidth:4.0];
[linePath stroke];
于 2012-09-25T08:40:50.787 回答
2

只需添加更多CGContextAddLineToPoint通话

- (void)drawRect:(CGRect)rect
{
    CGContextRef c=UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(c, 4.0);
    CGFloat red[4]={0.0f, 0.0f, 0.0f, 1.0f};
    CGContextSetStrokeColor(c, red);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 50.0f, 50.0f);
    CGContextAddLineToPoint(c, 100.0f, 100.0f);
    CGContextAddLineToPoint(c, 120.0f, 80.0f);
    CGContextAddLineToPoint(c, 140.0f, 120.0f);
    CGContextAddLineToPoint(c, 160.0f, 80.0f);
    ...

    CGContextStrokePath(c) ;
}
于 2012-09-25T08:28:07.447 回答
1

只需对您的代码进行一些更正:

-(void)drawLineFromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint{
    UIGraphicsBeginImageContext(YOUR_IMAGEVIEW.image.size);
    [YOUR_IMAGEVIEW.image drawInRect:CGRectMake(0, 0, YOUR_IMAGEVIEW.image.size.width, YOUR_IMAGEVIEW.image.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), startPoint.x, startPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), endPoint.x, endPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    [YOUR_IMAGEVIEW setImage:UIGraphicsGetImageFromCurrentImageContext()];
    UIGraphicsEndImageContext();
}

YOUR_IMAGEVIEW 是您正在绘制的图像视图的出口。

如您所见 - 您只需将起点和终点发送到此方法!像 1-2-3 一样简单。

编辑 1

如何使用它?声明一个全局 CGPoint “startPoint”;

然后说:

//___________________________________________________
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    startPoint = [touch locationInView:YOUR_IMAGEVIEW];
}
//___________________________________________________
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint nextPoint = [touch locationInView:YOUR_IMAGEVIEW];
    [self drawLineFromPoint:startPoint toPoint:nextPoint];
    startPoint = nextPoint;
}

编辑 2

这是另一种表达观点的方法:

- (void)drawGraphMethod{
    NSMutableArray *pointsArr = [[[NSMutableArray alloc]init]autorelease];
    //now you should add the needed points to your array
    //I have no idea what graph do you have, so I just
    //put there some random points that will look like a graph
    [pointsArr addObject:[NSValue valueWithCGPoint:CGPointMake(30.0, 10.0)]];
    [pointsArr addObject:[NSValue valueWithCGPoint:CGPointMake(40.0, 26.0)]];
    [pointsArr addObject:[NSValue valueWithCGPoint:CGPointMake(70.0, 55.0)]];
    [pointsArr addObject:[NSValue valueWithCGPoint:CGPointMake(80.0, 88.0)]];
    [pointsArr addObject:[NSValue valueWithCGPoint:CGPointMake(90.0, 33.0)]];
    [pointsArr addObject:[NSValue valueWithCGPoint:CGPointMake(130.0, 100.0)]];
    //well, you can store only objects, that's why I used NSValue.
    //it's not hard to cenvert it back

    //now parse the array
    for (int i = 0; i < pointsArr.count-1; i++) {
        CGPoint startPoint = [[pointsArr objectAtIndex:i] CGPointValue];
        CGPoint nextPoint = [[pointsArr objectAtIndex:i+1] CGPointValue];
        [self drawLineFromPoint:startPoint toPoint:nextPoint];
    }
}
于 2012-09-25T08:20:40.047 回答