last day (27 days later) » 

8:01 AM
-1
Q: Can hitTest intercept the responder chain of views?

PlateReverbI am trying to duplicate the basic concept of the highest voted answer to this question (in Swift): Slide from one button to another without lifting your finger I have duplicated all of the code, and I understand how hitTest and the responder chain work, I think... But the section of code in @Ch...

 
Please post the code you are running (if it is derived from another SO article, add a citation). Please describe what you expect to happen, along with NSLog or other evidence of what's really happening. I would steer clear of metaphorical words like "intercept", and state things in terms of functions you expect to be invoked, variable values you expect to find, etc.
 
@danh I updated it with the code that the previous answer linked to (now in Swift) says should "intercept" the hitTest & responding chain from a superview.
 
What the other answer seems to mean by "intercept" is that, by overriding hitTest and not calling it recursively for subviews, it guarantees that only that superview will be called with touches. If those subviews are simple UIView (not, for example, UIControls), or if they've had userInteractionEnabled set to false, then this override of hitTest is unnecessary.
 
@danh Also, to clarify... my subviews are UIButtons...
 
Right. They are buttons because you want the tap behavior of buttons? You're already handling some of their touches at the superview, why not also handle the simpler case (user taps one key) at the superview level too? Then the buttons can be either UIViews (lots more flexibility to decorate them visually) or buttons (if you like how those look) with user interaction disabled.
 
8:01 AM
@danh I have a fairly complicated IB setup with 128 buttons currently, so I may switch to UIViews now that I see the simplicity, I'm just trying to avoid that if possible because it will take some time to redo all the constraints, etc. The main thing I am trying to accomplish here is to get a CGPoint from my superview AND then be able to translat that to the UIButton (or UIView) underneath, which is then stored in as a value in a dictionary (the key is UITouch)... this is necessary for the type of "slide" touch down/touch up I am attempting to implement.
 
@danh The thing is, we've been up and down this road already with this OP: stackoverflow.com/questions/40811057/… I made this same suggestion in my answer: give up the whole "button" idea so that you can handle the gesture.
 
@matt This is a separate problem then the one you answered a few days ago. I am trying to determine if the hitTest function I posted above should be able to break the responder chain (so my superview can then call hitTest again in beganTouches to identify the underlying UIButton or UIView to store as a value in a Dictionary)
 
I agree with @matt 's prior answer. And with non-userInteractionEnabled buttons (or better, non-UIControls) you can not override hitTest, and certainly call it in your touch code to determine which subview is being touched.
 
"This is a separate problem then the one you answered a few days ago" Only because you've now gone off on an irrelevant tangent. You've ignored the conclusions we came to and are now going down a blind alley.
 
@danh Thanks. That answers my question. Out of curiosity, does the previous question/answer I linked to make sense to you? The answer has 5 upvotes, and seems to involve overriding hittest from a superview... (or maybe I misread or misunderstand Christopher's answer)?
@matt Yes. I was trying to determine if this was a blind alley or not, and it seems it is. However, does the answer I linked to (with 5 upvotes) make sense to you? It seems to involve in overriding hitTest from a superview, which isn't possible. Anyway, thanks for the help. I had a back-up plan, but I just wanted to see if I could implement Christopher's solution, because it would be quicker/simpler if it did work...
 
8:01 AM
I do not know what "overriding hitTest from a superview means"; you can override it anywhere down the hit-testing chain, from the UIWindow level on down. This is straightforward stuff. My advice is: If you don't understand hit testing, learn about it. If you don't understand gesture recognizers, learn about them. If you don't understand the responder chain and touch delivery, learn about it. It will then be obvious to you how to proceed cleanly.
 
@matt I have been studying all of those things a lot the past week. Maybe I misunderstood Christopher's answer. Does it make sense to you? Based on your response and danh's response to my question, I'm honestly not sure if his answer was correct, despite it's 5 upvotes... (his answer is linked to in the first line of my question above)
 
Well obviously I think he is right to say "I think a simpler solution is to create a superview that intercepts all the touches and decides what to do" because that is what I said.
 
@matt Indeed. I was more curious of your thoughts on his use of 'hitTest' in his solution. I'm definitely interested in using the simpler solution. Thanks again for your help!
 
The way I would use hitTest, if you are going to use it at all, is just to call it in your gesture recognizer so that you know whether the user's finger is in a button (as I do in this example github.com/mattneub/Programming-iOS-Book-Examples/blob/… — which, granted, is a tap, not a pan, but the principle is the same). But since you know where the buttons are and where the touch is, both in the superview's coordinates, there is actually no big need to do even that.
 
@matt Ok. I already have this problem on the way to being solved. I've implemented a lower level solution than you're recommending, which may or may not be necessary (touchesBegan, etc.) and I was planning on using the coordinates to access the buttons (or UIViews, if I replace them). I was just confused by Christopher's proposed solution of using hitTest within touchesBegan to access the UIButton of the subview directly (thus not needing to deal with the coordinates), but I'll complete this the way I think I know how. My apologies for the convoluted question.
To anybody who reads this in the future, hoping to make sense of Christopher's explanation... I gave up on it. I spent ~3 hours trying to make sense of it, and finally solved the same problem with a simple XYcoordinate parsing function in less than an hour...
@danh Thank you for your help, however, I just got a down voted answer because I quoted your last comment, which I assumed was correct. I just want to clarify that @matt says you can override hitTest and call it in your touch code, he has a link to a chapter in his book above that demonstrates this with gesture recognizers.
@matt I will study your example later. Thank you for your help. If you could please correct my answer or write your own answer for me to accept that would be great.
@matt It does not feel fair that I lost 2 points for asking my question in the best way I knew how to phrase it, and answering it with the information given to me. Your textbook answer I find difficult to follow and it does not fully demonstrate why the answer Christopher gave can work. It seems like a different use of hitTest than Christopher implemented within the touchesBegan function. Or is it not?
 

  last day (27 days later) »