2012-04-24 3 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 개의 객체가 겹치면 내 touchesbegan이 잘못된 메소드를 트리거합니다.

그래서 anyObject 대신 특정 객체에만 응답하는 여러 개의 UITouches를 정의하고 싶습니다. 어떻게하면 UITouch를 으로 정의해야합니까?은 가변 배열의 객체로 작동합니까?

+0

는 미안하지만 그 질문은 매우 혼란이다. 동시에 두 개의 다른 객체를 만지면 객체를 "선택"할 수 있도록 여러 개의 터치를 처리 하시겠습니까? 또는 터치가 "타격을 가할 수있는"모든 객체를 "선택"하는 단일 터치를 원하십니까? 아니면 둘 다요? 어쩌면 나는 더 혼란스럽고 당신은 둘 다 원하지 않을 것입니다. –

+0

다시 시도해 보겠습니다. 내 배열에 원이 있습니다. 해당 서클 중 하나라도 만진 경우 메서드 A를 수행하려고합니다. 사용자가 서클 중 하나 (I.E.보기 자체)를 건드리지 않으면 메서드 B를 수행하려고합니다. –

답변

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

감사합니다. 이해할 것이라고 확신합니다. 코드를 설명해 주시겠습니까? 그래서 당신은 세트를 만듭니다. 그 세트에 탭 단추를 추가하십시오. 그래서 다음 메쏘드에서 그 세트에 객체가 있는지 검사하고 그것이 사실이라면 그것을 실행합니다. –

관련 문제