2012-04-24 9 views
0

のオブジェクトでタッチするように変更するので、UIViewsである複数の円を保持する可変配列があります。 今私は私のタッチがこのようなメソッドの設定を始めた。[touches anyObject]を配列

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 

    CGPoint touchLocation = [touch locationInView:self.view]; 

    for (Circle *circle in playerOneCircles) 
    { 
     if ([circle.layer.presentationLayer hitTest:touchLocation]) 
     { 
      [circle playerTap:nil]; 
      break; 
     } 
    } 
} 

これは問題なく動作します。重複するビューに問題があります。 他のUIビューもtouchesbeganメソッド(他のメソッドをトリガーする)にも応答します。 2つのオブジェクトが重なると、私のタッチベーガンは間違った方法を引き起こします。

私はanyObjectではなく特定のオブジェクトにのみ応答する複数のUITouchesを定義したいと思います。どうすればUITouchをに限定しなければならないのですか?は私の可変配列のオブジェクトでしか動かないのですか?

+0

をcontainsPointを実装します。複数のタッチを処理したいので、同時に2つの異なるオブジェクトに触れることでそれらを選択しますか?または、タッチが「打つ」ことができるすべてのオブジェクトを「選択」するワンタッチをしたいですか?それともどちらか?たぶん私はさらに混乱していて、あなたはどちらも望んでいないでしょう。 –

+0

もう一度お試しください: 私の配列には円があります。これらのサークルのいずれかがタッチされている場合は、メソッドAを実行したいと思います。ユーザがサークル(I.E.ビュー自体)に触れていない場合、メソッドBを実行します。 –

答えて

1

EDIT

はコードを説明するためにあなたのコメントに答えるために、コメントを追加しました。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // We want to find all the circles that contain the touch point. 
    // A circle does not have to be "on top" (or even visible) to be touched. 
    // All circles that contain the touch point will be added to the touchedCircles set. 
    NSMutableSet *touchedCircles = [NSMutableSet set]; 

    // To support multiple touches, we have to look at every touch object. 
    for (UITouch *touch in touches) { 
     CGPoint touchLocation = [touch locationInView:self.view]; 
     // Search through our collection of circle objects. Any circle that 
     // contains this touch point gets added to our collection of touched 
     // circles. If you need to know which UITouch is "touching" each circle, 
     // you will need to store that as well. 
     for (Circle *circle in playerOneCircles) { 
      if ([circle containsPoint:touchLocation]) { 
       [touchedCircles addObject:circle]; 
      } 
     } 
    } 

    // We have completed our search for all touches and all circles. If 
    // and circle was touched, then it will be in the set of touchedCircles. 
    if (touchedCircles.count) { 
     // When any circle has been touched, we want to call some special method 
     // to process the touched circle. Send the method the set of circles, so it 
     // knows which circles were touched. 
     [self methodAWithTouchedCircles:touchedCircles]; 
    } else { 
     // None of our circles were touched, so call a different method. 
     [self methodB]; 
    } 
} 

あなたは、私は申し訳ありませんが、その質問は非常に混乱して...このようなサークルの何かのために

- (BOOL)containsPoint:(CGPoint)point 
{ 
    // Since each of our objects is a circle, to determine if a point is inside 
    // the circle, we just want to know if the distance between that point and 
    // the center of the circle is less than the radius of the circle. 
    // If your circle is really contained inside a view, you can compute the radius 
    // as one-half the width of the frame. 
    // Otherwise, your circle may actually have its own radius property, in which case 
    // you can just use the known radius. 
    CGFloat radius = self.frame.size.width *.5; 

    // This is just the Pythagorean Theorem, or instance formula. 
    // distance = sqrt((x0-x1)^2 + (y0-y1)^2) 
    // and we want to check that 
    //  distance < radius 
    // By simple algebra, that is the same as checking 
    //  distance^2 < radius^2 
    // which saves us from having to compute the square root. 
    CGFloat diffX = self.center.x - point.x; 
    CGFloat diffY = self.center.y - point.y; 
    return (diffX*diffX) + (diffY*diffY) < radius*radius; 
} 
+0

感謝します。ちょうど私が理解していると確信しています、あなたはコードを説明する気になりますか?あなたはセットを作成します。そのセットにタップしたボタンを追加します。次のメソッドでは、そのセットにオブジェクトがあるかどうかをチェックし、それが本当であれば実行します。 –

関連する問題