2014-12-27 1 views
4

보기 컨트롤러에서 self.view에 UITapGestureRecognizer를 추가합니다. 그리고 self.view 위에 작은보기를 추가합니다. 작은 뷰를 탭하면 self.view에서 UITapGestureRecognizer 이벤트를 트리거하고 싶지 않습니다. 여기 내 코드는 작동하지 않습니다.iOS : 상단보기에서 하단보기로 탭 제스처 이벤트 가로 채기

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UITapGestureRecognizer *_tapOnVideoRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleControlsVisible)]; 

    [self.view addGestureRecognizer:_tapOnVideoRecognizer]; 

    UIView *smallView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; 
    smallView.backgroundColor=[UIColor redColor]; 
    smallView.exclusiveTouch=YES; 
    smallView.userInteractionEnabled=YES; 

    [self.view addSubview:smallView]; 
    } 

    - (void)toggleControlsVisible 
    { 
     NSLog(@"tapped"); 
    } 

작은보기를 탭하면 self.view에서 여전히 tap 이벤트가 트리거됩니다. Xcode는 "탭된"상태로 기록합니다. smallView에서 self.view로 제스처 이벤트를 인터셉트하는 방법?

답변

7

UIGestureRecognizer 대표 구현 방법 shouldReceiveTouch 이와 유사합니다. 터치 위치가 topView 안에 있으면 터치하지 마십시오.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{ 
    CGPoint location = [touch locationInView:self.view]; 

    if (CGRectContainsPoint(self.topView.frame, location)) { 
     return NO; 
    } 
    return YES; 
} 
+0

고맙습니다. 이것은 잘 작동합니다. 그런데 왜 smallView가 UIButton처럼 탭 이벤트를 인터셉트 할 수 없습니까? –

+0

@ nimingzhe2008 이렇게하려면'smallView'에'UITapGestureRecognizer'를 추가해야합니다. (차이점은'UIButton's에는 기본적으로'UITapGestureRecognizer'가 내장되어 있습니다.)하지만 gabbler의 솔루션은 훨씬 깔끔합니다. –

+0

린지 스코트 (Lyndsey Scott)가 언급 한 것과 같이 작동합니다. 독점적 인 터치 뷰는 동일한 창에 다른 뷰가 연결되어 있지 않은 경우에만 터치를받습니다. exclusiveTouch보기가 터치를 받으면 해당 터치가 존재하는 동안 동일한 창에서 다른보기가 터치를받지 않습니다. – gabbler