How would you remake UIControl? How does he fit into the defendant chain?

If I were to rewrite UIKit, how would I implement UIControl?

I am having trouble connecting UIControl to my conceptual understanding of touch and responder chain. In particular, I am confused about who is in charge of calling UIControl calls begin

, continue

and endTrackingWithTouch:withEvent

.

My naive interpretation would be that UIControl is responsible for handling touch events such as any other subclass of UIResponder in touchesBegan:

and its associated methods. However, this is complicated because the touch event remains associated with the view that handles it first, and this would create a situation where a touch that occurred on the button but continued as a panning across the screen would continue to be handled by that button, which seems counterintuitive. I could imagine that in this case, the button might start forwarding these touch events to its supervisor, but that seems ugly. It's also a little confusing because of UIControlEvents like touchUpInside, which assume strokes are processed elsewhere, and UIControl is only notified of subtle events.

My best guess, here, is that UIControls are handled differently when the view-heirarchy is scanned to find the responder, and that the top-most check of the control responders of any of its subzones are UIControls and then calls the correct ones as needed, but it also seems a little odd.

Does anyone have any guidance or clarification on this issue? I dug around the documentation a bit but couldn't find anything.

+3


source to share


1 answer


UIControl

is a subclass of UIView so it can detect user interactions and uses the target action pattern to call some methods / actions on objects / targets.

As you said, in Apple's way: "An event travels along a specific path looking for an object to handle it."

So, the main workflow for UIControl

:

  • UIControl

    detects major events ( UIControlEventTouchDown

    , UIControlEventTouchCancel

    ...), the event is handled by the responder's workflow chain.

  • addTarget:action:forControlEvents:

    tells you to UIControl

    invoke an action on the target for the event type argument.

  • When the event type and target match, the action will be performed.

EDIT

UIControl

is just an interface for providing target / action pattern behavior for objects that implement it.



So what really happens when you touch the screen, there is a base UIEvent that is created with a timestamp, type (Touches, Motion or RemoteControl) and subtype.

iOS will collect into this event everything UITouch

associated with this event (one UITouch

= action with one finger). UITouch contains UIView

both UIWindow

where touch happens and other things.

There are three functions in UIEvent:

  • allTouches()

    : returns a NSSet

    fromUITouch

  • touchesForView(aView)

    : returns NSSet

    UITouch

    for a specificUIView

  • touchesForWindow(aWindow)

    : returns NSSet

    UITouch

    for a specificUIWindow

From my point of view, when an event is fired, the system will dispatch it to yours UIApplication

and it will dispatch the event to the FirstResponderChain workflow.

So, I think that when the view hierarchy fails, it checks if the method returns touchesForView

something, if it does return something, then it calls the corresponding method in UIView

that implements UIResponder

), so it can call touchesBegan:withEvent:

eg. Otherwise it continues.

+1


source







All Articles