This question already has answers here:
How to add a touch event to a UIView?

(15个答案)


6年前关闭。




我有这个grayView,它禁用了控制器,并礼貌地回答了我之前问过的问题,但是我需要检测是否触摸了grayView以及是否触摸了grayView。如果有人可以帮助我找出解决方案,我将深表感谢。我尝试使用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];
}

最佳答案

您可以在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];
}

08-25 19:49