2016-10-01 72 views
1

我有兩種看法。一個是包含子視圖的透明視圖。我只想在單擊父視圖時刪除screenView。當我點擊popUpView時,我不想撥打tapGesture。如何檢查?如何檢查其父視圖或子視圖觸摸手勢事件時是否被調用?

screenView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; 
screenView.backgroundColor = [UIColor clearColor]; 
UITapGestureRecognizer * clearTable = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clearTableViewAction:)]; 
[clearTable setNumberOfTapsRequired:1]; 
[screenView setUserInteractionEnabled:YES]; 
[screenView addGestureRecognizer:clearTable]; 
screenView.tag = 100; 
[self.view addSubview:screenView]; 

self.popUpView = [[UIView alloc]init]; 
self.popUpView.frame = ... 
self.popUpView.backgroundColor = WhiteColor; 
self.popUpView.userInteractionEnabled = YES; 
self.popUpView.tag = 200; 
[screenView addSubview:self.popUpView]; 

-(void)clearTableViewAction:(UITapGestureRecognizer*)sender { 
    if(sender.view.tag == 100){ 
     [UIView animateWithDuration:0.2 
        animations:^{screenView.alpha = 0.0;} 
        completion:^(BOOL finished){ [screenView removeFromSuperview]; 
     }]; 
    } 
} 
+0

If userInteractionEnabled as NO。仍然TapGesture被調用。 – Balasubramanian

回答

1

使用shouldReceiveTouch委託方法檢查:

一樣,

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{ 
    if ([touch.view.tag == 100) 
    { 
     return YES; 
    } 
    else 
    { 
     return NO; 
    } 
} 

現在,clearTableViewAction手勢的方法將不會被調用,如果你將在popUpView點擊。

+1

非常感謝。它的工作。簡單而整齊。 – Balasubramanian

+0

很棒... HTH:)@ Balasubramanian –

相關問題