Reputation: 5972
I have a somewhat complex view hierarchy. The problem I am having is that I have several views that are siblings, and these views can be moved around on screen. My code does some subview re-ordering whenever a view is moved so that it becomes visually on top of all the other siblings.
My problem is that when a view is visually on top of the other one, the view below it the one that actually receives the touch first. Does anybody know what could cause behaviour like this? Perhaps someone has experienced a similar issue.
Thanks in advance,
Upvotes: 1
Views: 243
Reputation: 799
That kind of situation might happen if you change the zPosition of the layer of the views.
Upvotes: 0
Reputation: 5972
Ok I just figured out the problem, so I'm gonna answer my own question in case anybody else stumbled upon this.
The problem here turned out to be with on of my superviews, in which these siblings were sitting in. This superview was overriding the hitTest: method and looping over its subviews in forward order. Apple clearly states that the hitTest: method should loop through its subviews in reverse order. So updating the loop to go in reverse order fixed the problem.
The reason to go in reverse order is simple. Each time a subview is added, it is added to the end of the subviews array, and is visually placed on top of all other subviews. So the front most view on screen is the last view in the subviews array and that is why you need to go in reverse order.
Hope this helps anyone else who stumbles on this behaviour.
Upvotes: 1
Reputation: 14010
It is my understanding that the hitTest
for the superview of these views is simply iterating through its subviews
array and choosing the first one that responds with a non-nil
. Views ordered earlier in the subviews
list are drawn first, meaning that they will be visually underneath other views.
Upvotes: 1