Why is hitTest:withEvent: called three times for each touch? Why is hitTest:withEvent: called three times for each touch? ios ios

Why is hitTest:withEvent: called three times for each touch?


I had the same problem and was able to solve it with this code. Even though pointInside and hitTest get called 3 times, touchesBegan (or touchesEnded) of the UIView that was touched only gets called once.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{       if (event.type == UIEventTypeTouches)        NSLog(@"%@", self);}- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{    if ([self pointInside:point withEvent:event])        return self;    return [super hitTest:point withEvent:event];}- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{    if (CGRectContainsPoint([self bounds], point))    {        if (event.type == UIEventTypeTouches)        {                       return YES;        }    }    return NO;}


Do you have more than one subview?

From the docs:

This method traverses the view hierarchy by sending the pointInside:withEvent: message to each subview to determine which subview should receive a touch event. If pointInside:withEvent: returns YES, then the subview’s hierarchy is traversed; otherwise, its branch of the view hierarchy is ignored. You rarely need to call this method yourself, but you might override it to hide touch events from subviews.


Yes, it’s normal. The system may tweak the point being hit tested between the calls. Since hitTest should be a pure function with no side-effects, this should be fine.

Refer to Apple Mailing List: Re: -hitTest:withEvent: called twice?