0

私はこの問題で立ち往生しています。前の終点と次の始点の間に線を引く必要があります。2点の間に線を引くための私のコードは

- (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は、Core Graphicsが線や曲線の描画などの単純なものを要求するのではなく、より高いUIKitメソッドを使用することを好むため、次のようにメソッドを書き直すことができます。

[[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は、描画している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 に答える