2012-09-25 49 views
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) ; 
} 

我該如何給出多個點,以便我可以將它顯示爲一個圖形。 在此先感謝

回答

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) ; 
} 
+0

感謝的人,90%解決。但是,我從數組中獲得x,y值。所以我怎麼能實現這個數組到這些座標。 – Joker

+0

您需要使用循環: 'CGContextMoveToPoint(c,50.0f,50.0f); for(int i = 0; i zakhej

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; 
} 

EDIT 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]; 
    } 
} 
+0

你能解釋我多一點點PLZ ...喜歡爲什麼YOUR_IMAGEVIEW。 – Joker

+0

感謝的人,但用戶沒有選擇觸摸n繪製。當打開視圖時它會自動自動完成。 – Joker

+0

好吧,不知何故,你會得到這些點 - 曲線的路徑,不是嗎? –

3

在一個附帶說明,蘋果更喜歡使用更高的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]; 
+0

是的,UIBezierPath對於非常複雜的東西來說比較慢,但是對於幾行來說,它更容易使用,並且和CG繪圖一樣強大。此外,還有更多的預建選項(如圓角矩形和closePath等)。 – Fogmeister

+0

這隻能在drawRect方法中工作嗎? –

相關問題