2011-06-21 72 views
1

我想在我的應用程序中實現和設計建築物地板的地圖。在開始之前,我想提供一些建議。如何繪製UIBezierPath並與之交互

我打算使用UIBezierPath繪製形狀。每個UIBezierPath將在我的地圖上代表一家商店。 這裏是一個例子(map_with_UIBezierPath

我的代碼結構如下:我有一個UIViewController和一個UiView。在UIViewController的「viewDidLoad中」的方法,我實例化的UIView和UIView的「的drawRect」的方法,我得出的形狀像以下(UIBezierPathExtension從UIBezierPath繼承):

- (void)drawRect:(CGRect)rect { 

context = UIGraphicsGetCurrentContext(); 

[[UIColor grayColor] setFill]; 
    [[UIColor greenColor] setStroke]; 

UIBezierPathExtension *aPath = [[UIBezierPathExtension alloc] init]; 
aPath.pathId = 1; 
    [aPath moveToPoint:CGPointMake(227,34.25)]; 
[aPath addLineToPoint:CGPointMake(298.25,34.75)]; 
[aPath addLineToPoint:CGPointMake(298.5,82.5)]; 
[aPath addLineToPoint:CGPointMake(251,83)]; 
[aPath addLineToPoint:CGPointMake(251,67.5)]; 
[aPath addLineToPoint:CGPointMake(227.25,66.75)]; 
    [aPath closePath]; 
aPath.lineWidth = 2; 
[aPath fill]; 
[aPath stroke]; 
[paths addObject:aPath]; 

UIBezierPathExtension* aPath2 = [[UIBezierPathExtension alloc] init]; 
aPath2.pathId = 2; 
[aPath2 moveToPoint:CGPointMake(251.25,90.5)]; 
[aPath2 addLineToPoint:CGPointMake(250.75,83.25)]; 
[aPath2 addLineToPoint:CGPointMake(298.5,83)]; 
[aPath2 addLineToPoint:CGPointMake(298.5,90.25)]; 
[aPath2 closePath]; 
aPath2.lineWidth = 2; 
[aPath2 fill]; 
[aPath2 stroke]; 
[paths addObject:aPath2]; 

    ... 
} 

我也實施平移和縮放手勢在UIViewController中。

現在,我問我如何與每一個形狀交互。我想檢測一下它,改變他的顏色並在選定的形狀上顯示一個像that這樣的菜單。

有人可以告訴我正確的方向嗎?

THX提前

回答

2

您需要尋找您的視圖觸摸事件(的touchesBegan,TouchesMoved,TouchesEnded,TouchesCancelled)。當你接觸時,你可以在視圖中詢問它的位置。你可以使用這個位置來測試這個點是否在你的任何路徑之內,如果是的話,做你喜歡的東西。

使用您的示例代碼,這裏可能是一個粗略的touchesBegan ...

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    for (UITouch *touch in touches) { 
     CGPoint pointTouched = [touch locationInView:self]; 
     for (UIBezierPath *path in paths) { 
      if ([path containsPoint:point]) { 
       // do something cool with your path 
       // or more likely, set a flag to do the cool thing when drawing 
      } 
     } 
    } 
} 

不要忘記,你應該處理所有的觸摸事件,做一些理智的對待每一個。此外,上面的代碼具有多點觸控功能,但您可能只想進行一次觸控,在這種情況下,您可以通過多種方式來消除「觸控」循環。