Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hitTest:withEvent: Not Working

Tags:

ios

iphone

I'm making an app where I have a background view and that has six UIImageView's as subviews. I have a UITapGestureRecognizer to see when one of the UIImageViews is tapped on and thie handleTap method below is what the gesture recognizer calls. However, when I run this, the hitTest:withEvent: always returns the background view even when I tap on one of the imageViews. Does it have something to do with the event when I call hitTest?

Thanks

- (void) handleTap: (UITapGestureRecognizer *) sender
{
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        CGPoint location = [sender locationInView: sender.view];
        UIView * viewHit = [sender.view hitTest:location withEvent:NULL];
        NSLog(@"%@", [viewHit class]);
        if (viewHit == sender.view) {}
        else if ([viewHit isKindOfClass:[UIImageView class]])
        {
            [self imageViewTapped: viewHit];
            NSLog(@"ImageViewTapped!");
        }
    }
}
like image 575
Jai Srivastav Avatar asked Apr 22 '12 19:04

Jai Srivastav


1 Answers

UIImageView are, by default, configured to not register user interaction.

From the UIImageView documentation:

New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.

So, right after you initialize your views you should have:

view.userInteractionEnabled = YES;

This will turn the interaction back on and you should be able to register touch events.

like image 112
C4 - Travis Avatar answered Oct 22 '22 11:10

C4 - Travis