Search code examples
iosobjective-cuiviewtouch

I need to detect touch on a certain view


I have this grayView that disables the controller and makes it look inactive courtesy to a previous question I asked, but I need to detect if grayView is touched and if it touched get rid of the grayView. If anyone can help me figure out a solution to this I would deeply appreciate it. I tried using UITouchedBegan and many other stackoverflow solutions but each one didn't work. Any help will be appreciated.

grayView is an UIView.

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

Solution

  • you can add a UITapGestureRecognizer to the grayView, and when touched, remove the grayView from superview.

    Here's how I would do it.

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

    Make sure you add this code before you add your grayView to your superView

    Then,

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