2011-11-23 51 views
2

我用Core Graphics繪製了多邊形。但我無法調整多邊形的大小。我用UIBezierPath繪製多邊形。這是我的代碼如何在iPhone中繪製可調整大小的多邊形?

CGPoint gestureStartPoint,currentPosition; 
- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     currentPath = [[UIBezierPath alloc]init]; 
     currentPath.lineWidth=1; 
     xx1 = 30; 
     yy1 = 30; 
     xx = 30; 
     yy = 30; 
     CGPoint gestureStartPoint,currentPosition; 
    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect { 
    if(drawColor==nil){ 
     [[UIColor redColor]setStroke]; 
     [currentPath stroke]; 
    } 
    else { 
     [drawColor setStroke]; 
     [currentPath stroke]; 
    } 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
     UITouch *touch = [touches anyObject]; 
     gestureStartPoint = [touch locationInView:self]; 
     currentPosition.x = xx; 
     currentPosition.y = yy; 
     xx = gestureStartPoint.x; 
     yy = gestureStartPoint.y; 
     [currentPath moveToPoint:(currentPosition)]; 
     [currentPath addLineToPoint:(gestureStartPoint)]; 
     [self setNeedsDisplay]; 
} 

這是樣本resizable Polygon的鏈接。如何用這種可調整大小的屬性繪製多邊形?我不知道從哪裏開始製作可調整大小的多邊形。

+0

我可以有你的電子郵件..必須問一些問題。? – mAc

+0

嘿! Thanx ...你會添加到Gmail中 – mAc

回答

1

這比簡單地調用一些CoreGraphics魔術更復雜。

要簡單地複製您鏈接到該網站上的邏輯,我想通過分解問題開始:

兩種類型的手勢識別:水龍頭,挖掘 - 保持 - 拖拽。

點擊應該添加一個x,y(點)到您正在存儲和重繪的點列表。

點按住拖拽應該使用用戶水龍頭的x,y位置來確定最近的頂點 - 並且您應該也可以進行一些最大距離檢查。一旦確定了用戶正在「拖動」哪個頂點,您可以在列表中操縱該點並重繪。