2010-05-08 4 views
28

하나의 큰 UI 뷰 위에 3 개의 UIView가 있습니다. 나는 사용자가 맨 위에있는 것을 터치하고 다른 것들에 관심이 없는지 알고 싶다. 두 번째 UIView에 두 개의 버튼이 있고 세 번째 UIView에 UITable이 있습니다.특정 UIView가 다른 UIView들 사이에서 터치되었는지 감지합니다.

문제는 첫 번째보기에서 userInteractionEngabled를 켜고 작동하지만 다른 모든보기가 꺼져 있어도 같은 방식으로 응답합니다. self.view에서 userInteractionEnabled를 비활성화하면 응답하지 않습니다. 또한 touchesBegan 대리자 메서드에서 어떤 뷰가 수정되었는지 감지 할 수 없습니다.

내 코드 : 위해

UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)]; 
aView = userInteractionEnabled = YES; 
[self.view addSubview:aView]; 

UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 320, 50)]; 
bView.userInteractionEnabled = NO; 
[self.view addSubview:bView]; 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
//This gets called for a touch anywhere 
} 

답변

58

다른 뷰 내부의 특정보기는 그러나 hitTest를 사용할 수있는 터치되었는지 여부를 확인합니다.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; 

사용자 정의 touchesBegan은 터치 세트의 모든 터치를 확인합니다. 그러나 hitTest 방법의 포인트는보기가 수퍼 (다른 뷰를 포함하는 것)이다

- (CGPoint)locationInView:(UIView *)view; 

방법을 사용하여 얻을 수있다.

편집 :이 도움이되었다 희망

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGPoint locationPoint = [[touches anyObject] locationInView:self]; 
    UIView* viewYouWishToObtain = [self hitTest:locationPoint withEvent:event]; 
} 

, 폴 내 경우

+0

죄송합니다. 귀하가 올바른 길을 가고 있음을 알고 있지만 귀하의 말을 정확히 이해하는 데 약간의 어려움이 있습니다. 귀하의 locationInView에서 감동에서 지점을 만지고 나는 기술적으로 어떤 uiview 사용자 가이 감동하지만, 어떻게 hitTest를 호출하는지 잘 모르겠어요 – Rudiger

+2

그것은'locationInView :'매개 변수와' nil'은 메인 윈도우의 좌표 공간에서'CGPoint'라는 위치를줍니다. –

26

내가 발견하고 싶은있는 UIView는 그러나 hitTest에 의해 반환되지 않은 : 여기에 빠른 사용자 정의 구현입니다. 의 감동을보기 원하는 하나입니다

CGPoint locationPoint = [[touches anyObject] locationInView:self.view]; 
    CGPoint viewPoint = [myImage convertPoint:locationPoint fromView:self.view]; 
    if ([myImage pointInside:viewPoint withEvent:event]) { 
     do something 
    } 
+0

나는 또한 대답을 받아 들였습니다. 어떤 이유에서이 방법도 효과가있었습니다. UIImageView에서 히트를 탐지하려고 시도했지만 UIView가 관련이 없다면이를 탐지하려고했습니다. –

8

에서 touchesBegan 확인을 나는 사용자가 특정 이미지 뷰을 만지면 확인을 시도 할 때 나를 위해 문제를 해결 :하지만 다음 코드는 잘 작동했다.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    if ([touch view] == imageView) { 
     ... your code to handle the touch 
    } 
    ... 

당신이보기 diamensions에 대한 내용을 조정하면

그것도 지역에 터치 이벤트를 인식 따라 달리보기 크기를 변경해야 할 경우가보기에 매우 만족한다. 내 경우에는 이미지 비율을 으로 유지하려고 시도했을 때 UIViewContentModeScaleAspectFit "공백"영역에서 필요에 따라 이미지가 조정되었지만 touch 이벤트도 catch되었습니다.

12

다음과 같은 방법으로도 수행 할 수 있습니다. 이 범위 내에 있다면 그런

BOOL withinBounds = CGRectContainsPoint(viewYouCareAbout.bounds, location); 

: 한 지점이 뷰의 범위 내에있는 경우

CGPoint location = [touches.anyObject locationInView:viewYouCareAbout]; 

은 다음을 참조 : 관심있는 뷰에서 터치의 위치를 ​​찾을 수

우선 당신의 행동을 수행하십시오.

CGRectContainsPoint(viewYouCareAbout.bounds, [touches.anyObject locationInView:viewYouCareAbout]) 
8

SWIFT 코드 :

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     if let touch = touches.first { 
      if touch.view == self.view { 
       self.hideInView() 
      } else { 
       return 
      } 

     } 
    } 
+0

신속한 답변 최대 득표로 사랑을 나눕니다. –

0

이 나를 위해 일한 :

예를 들어, if 문에서 직접 사용할 수있는 하나 개의 문장으로 수행 할 수 있습니다
func respondToGesture(gesture: UIGestureRecognizer) { 
    let containsPoint = CGRectContainsPoint(targetView.bounds, gesture.locationInView(targetView)) 
} 
관련 문제