2011-06-21 3 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のようなメニューを選択した図形に表示したいと思います。

誰かが正しい方向を教えてくれますか?あなたはあなたのビューでタッチイベント(TouchesBegan、TouchesMoved、TouchesEnded、TouchesCancelled)を探すために必要な事前

答えて

2

Thxを。あなたが触れると、あなたの視点でその場所を尋ねることができます。この場所を使用して、そのポイントがあなたのパスに含まれているかどうかをテストすることができます。あなたの例のコードを使用して

、ここ

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

はあなたがすべてのタッチイベントを処理し、それぞれに賢明な何かを行う必要があることを忘れないでください...ラフTouchesBeganかもしれません。また、上記のコードはマルチタッチ対応ですが、ワンタッチでも可能です。この場合、「タッチ」ループを排除する方法があります。

関連する問題