2012-02-29 58 views
0

如何在兩個形狀之間繪製線連接器,以便該線以適當的角度繪製並隨着形狀的任何移動一起移動?iPhone:連接到形狀的線連接器

事情是這樣的:

enter image description here

我想象UIBezier曲線正是我需要的,但任何教程或入門,將不勝感激幫助。

+0

你是否得到了連接它們的邏輯 - 什麼時候繪製一條直線,何時繪製一個90度的直線? – Prathiba 2012-07-12 06:22:31

+0

號還沒有得到很遠。 – Jordan 2012-07-17 03:24:57

回答

0

我認爲UIBezierPath是正確的路要走,所以你一定要閱讀它。我寫了一個快速示例,讓我有更多的選擇,我不使用。

繪製路徑不是困難的部分。 你必須跟蹤你創建的所有框,並在它們之間存儲它們之間的連接。不知何故,它很大程度上取決於你如何存儲框,但關係數據庫感覺是正確的解決方案。鑑於這些對象和連接,您將爲您的某個視圖生成正確的路徑。

- (void)drawRect:(CGRect)rect { 

    // say we already created a "Make" box 
    UIBoxThing *make = ... 
    // and here we already created a "Diagrams" box 
    UIBoxThing *diagrams = ... 

    [[UIColor blackColor] setStroke]; 

    // since we know Diagrams and Make are connected (via some other data), we must draw an arrow between them 
    UIBezierPath *path = [[UIBezierPath alloc] init]; 
    path.lineWidth = 2; 

    // the midpoint of the right side of our first box 
    CGPoint start = CGPointMake(make.frame.origin.x+make.frame.size.width, make.frame.origin.y+(make.frame.size.height/2)); 

    [path moveToPoint:start]; 

    // the midpoint of the left size of our second box 
    CGPoint end = CGPointMake(diagram.frame.origin.x, diagram.frame.origin.y+(diagram.frame.size.height/2)); 

    [path addLineToPoint:end]; 

    [path stroke]; 
} 

這與代碼,它可以告訴我們,如果盒子是彼此或左右進行補充,並且可以彎曲與addCurveToPoint行:或多項上UIBezierPath其他方法。大部分路徑的東西取決於你的風格,沒有一個正確的風格來連接兩個點。

+0

我會接受這個答案。尋找一個很好的代碼示例。謝謝! – Jordan 2012-11-25 19:33:06