Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does UIScrollView steal touches from its subviews?

I'd like to be able to create my own container subclasses of UIView which can react first to touches, before their subviews. This is tricky because normally a subview receives touch events (via touchesBegan: etc) before superviews. How does UIScrollView reverse this?

To be clear, I am not asking how UIScrollView behaves. I understand what it does, and how you would normally use it. I'm asking about how I could cleanly implement my own version of this -- not because I want to, but because I'm trying to build reusable container views that take advantage of similar behavior.

like image 365
Tyler Avatar asked Mar 18 '10 17:03

Tyler


2 Answers

You can implement hitTest:withEvent: method in your UIView subclass.

This method gets called to check what subview must receive the touch event, so you can perform some action there before it actually happens. You can also change subview that must receive event.

like image 92
Vladimir Avatar answered Nov 09 '22 17:11

Vladimir


[Edit: Okay, I somehow read the question as the exact opposite what was actually asked. I'll leave this here in case someone is curious but it's not relevant to the actual question -- TechZen]

A scroll view has move both horizontally and vertically so it has to sample all touches to see if they are scrolling touches.

For example, you have a slider in scrollview that scrolls horizontally. The user touches the slider. Is the touch intended to slide the slider or is it the start of a horizontal scroll? Which should have precedence?

Even though programmatically, the responder chain moves from the bottom most view (top most visually) up the chain until it finds an object that deals with it, behind the scenes the processing of a touch's location goes from the top most view (which is the app window) downward. Most views simply forward this information to subviews who then activate then send an event up the responder chain to be handled. Because scrolling the entire view is a special event that doesn't really belong in the ordinary responder chain, a scrollview intercepts the down pass of the touch's location in order to determine if it needs to scroll before it passes the touch locations to subviews.

like image 45
TechZen Avatar answered Nov 09 '22 17:11

TechZen