1

I cannot find in the documentation how you call the hit test method? Is it called automatically as a delegate method? Or how would you call it? When I put the following code in a VC, it does not fire when you load or tap on the view. Thanks for any suggestions.

  - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    {
NSLog(@"hit test firing");
       return self.view;
    }
user6631314
  • 1,751
  • 1
  • 13
  • 44
  • It is a `UIView` method, not a `UIViewController` method. You can call it whenever you like. – Paulw11 Jan 14 '18 at 22:19
  • Sorry for my ignorance. What is the code to call it? – user6631314 Jan 14 '18 at 23:20
  • You would call it via `[someUIViewObject hitTest:pointYouWantToTest, withEvent: nil]`, but as per my comment on the answer, explain what you are actually trying to do. – Paulw11 Jan 15 '18 at 00:44
  • This previous question explains goal in more detail. I thought first step would be just to get method to fire.https://stackoverflow.com/questions/48251450/ios-objective-c-pass-touch-event-on-scrollview-to-view-below-using-hittouch – user6631314 Jan 15 '18 at 03:32
  • It called automatically.To understand the working of Hit test go through this document: http://smnh.me/hit-testing-in-ios/ – Pallavi Srikhakollu Jan 15 '18 at 07:06
  • Ok, if it is called automatically, where would I place above method so that it fires? - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { NSLog(@"hit test firing"); return self.view; } I tried placing it in viewdidload of VC, view willappear and so forth and it did not fire. I want to add some additional code but first step is to get it to fire at all. – user6631314 Jan 15 '18 at 14:28

1 Answers1

1

It is a method on UIView, here is the documentation: https://developer.apple.com/documentation/uikit/uiview/1622469-hittest

It will be called automatically by the system to determine which view has been tapped. You can override it if you want custom functionality.

Ric Santos
  • 15,419
  • 6
  • 50
  • 75
  • So if I put that method in a viewcontroller, it should get called automatically? Right now, it is not firing. – user6631314 Jan 14 '18 at 23:22
  • It will never be called for a UIViewController. It is a method on UIView, so it will only be called for UIView and its subclasses. – EricS Jan 14 '18 at 23:35
  • Where in the app should I place the code so that the NSLog statement will print out? I want to verify that it is getting called. – user6631314 Jan 14 '18 at 23:54
  • 1
    You would need to add it to a `UIView` subclass. If you haven't created any `UIView` subclasses that are in your view then there is nowhere you can add it. I think, perhaps, you have an X-Y problem here. What are you actually trying to achieve? If you want to record taps on your view controller's base view then you will need to add a tap gesture recogniser to it. – Paulw11 Jan 15 '18 at 00:43
  • I have a scrollview that is overlapping the bottom of a textview and absorbing touch events. There is a gesturerecognizer on both the scrollview for scrolling and the textview for touch events. Have already used canceltouchesinView = NO. I think I need to add hittest code so that the taps pass through to the textview. But have not been able to figure out where to put hittest code. I guess I have to create a category for scrollview? – user6631314 Jan 15 '18 at 03:25