2012-04-24 58 views
0

因此,我有一個可變數組,其中包含UIViews的幾個圓。 現在我有我的觸摸開始像這樣的方法設置。更改[觸摸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; 
     } 
    } 
} 

這工作正常。但它會產生重疊視圖的問題。 我希望其他UIviews也能夠響應touchesbegan方法(這會觸發其他方法)。但如果兩個對象會重疊,那麼我的touchesbegan會觸發錯誤的方法。

所以我想定義多個UITouches只響應某些對象而不是anyObject。我將如何將UITouch定義爲只有使用來自可變數組的對象?

+0

我很抱歉,但這個問題是非常混亂。你想處理多個觸摸,以便同時觸摸兩個不同的對象「選擇」它們?或者,你是否需要一次觸摸來「選擇」觸摸可能「擊中」的所有對象?或者兩者都有?也許我更加困惑,你也不想要。 – 2012-04-24 16:53:49

+0

我會再試一次: 我在我的陣列中有圈子。如果任何一個圓圈被觸摸,我想執行方法A.如果用戶沒有觸及任何圓圈(I.E.視圖本身),那麼我想執行方法B – 2012-04-24 17:51:46

回答

1

編輯

添加評論回答您的評論解釋代碼。

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

您將實現containsPoint一個圈這樣的事情...

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

謝謝我得到它的工作。我確信我明白,你是否願意解釋代碼?所以你創建一個集合。在那個設置中添加輕擊的按鈕。所以在下一個方法中,它會檢查該集合中是否存在一個對象,如果它爲真則執行它? – 2012-04-25 09:53:45