我需要检测某个视图上的触摸

时间:2013-10-31 17:37:45

标签: ios objective-c uiview touch

我有这个grayView禁用控制器,并使它看起来不活跃,因为我问过上一个问题,但我需要检测是否触及了grayView以及是否触及了grayView {1}}。如果有人能帮助我找到解决方案,我会非常感激。我尝试使用UITouchedBegan和许多其他stackoverflow解决方案,但每个解决方案都不起作用。任何帮助将不胜感激。

grayViewUIView

-(void)ViewDidLoad
{
    grayView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    grayView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    [self.navigationController.view addSubview:grayView];
}

1 个答案:

答案 0 :(得分:1)

您可以向UITapGestureRecognizer添加grayView,触摸后,从grayView移除superview

我将如何做到这一点。

//your gray view
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self   action:@selector(removeGrayView:)];
tap.numberOfTapsRequired = 1;
[_yourGrayView addGestureRecognizer:tap];

请确保在将grayView添加到superView

之前添加此代码

然后,

-(void) removeGrayView: (UITapGestureRecognizer) gesture {
     [_yourGrayView removeFromSuperView];
}