15

I have a custom UIView that implements hitTest:withEvent:

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    .... some code ....

    int x = 0;
    x = [[event allTouches] count];
    return [super hitTest:point withEvent:event];
}

The problem is that x is always 0. Do I have implement other APIs or configure the UIView to start getting the touches?

I only needed it in order to differentiate between touch start/move and end.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Jamil
  • 641
  • 1
  • 7
  • 17
  • What is shown by `NSLog(@"Event: %@", event);`? – Phillip Mills Jun 20 '12 at 12:38
  • Event: timestamp: 0 touches: {( )} Event: timestamp: 0 touches: {( )} Event: timestamp: 248999 touches: {( )} – Jamil Jun 21 '12 at 09:22
  • @PhillipMills hitTest gets called three times with the logs above – Jamil Jun 21 '12 at 09:24
  • It worth mentioning that this custom UIView is used as a top level container\view in UIViewController. To use it, I only modified the xib file of the viewcontroller – Jamil Jun 21 '12 at 09:25
  • 3
    Touches aren't added to the event until after hit testing succeeds. What is it that you're trying to accomplish? – jlehr May 21 '14 at 23:08
  • 1
    "Touches aren't added to the event until after hit testing succeeds." Then why is the view getting the touch-type event in the first place? If you check event.type, it is UIEvent.EventType.touches – Oscar Mar 27 '19 at 03:06

1 Answers1

-4

Try this

yourView.userInteractionEnabled = YES;

iOs provides the possibility to nullify the user interactions in the view hierarchies. Being said that in the chain of events on iOS it's possible to ignore the touch event in the most immediate view in the view stacks and then the next view in the hierarchy will try to take responsibility to respond to the interaction.

Being said all the previous it can be happening that the view that should respond to the user interaction is being set to NO, so the next view in the hierarchy will respond to the event. This can be the reason why it was not working for you.

artud2000
  • 544
  • 4
  • 9