【问题标题】:How can I check whether its parent view or subview when touch gesture event called?调用触摸手势事件时如何检查其父视图或子视图?
【发布时间】:2016-10-01 05:37:04
【问题描述】:

我有两种看法。一种是包含子视图的透明视图。我只想在单击父视图时删除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];
         }];
    }
}

【问题讨论】:

  • 如果将 userInteractionEnabled 设为 NO。 TapGesture 仍然被调用。

标签: ios objective-c uiview uitapgesturerecognizer


【解决方案1】:

使用shouldReceiveTouch委托方法并检查:

喜欢,

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

现在,如果您点击popUpView,则不会调用clearTableViewAction 手势方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 2012-10-10
    相关资源
    最近更新 更多