UIView touch event in controller
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Touch handling in iOS can live in a UIView, a UIViewController, or a gesture recognizer, but those options are not interchangeable. If the interaction belongs to one specific visual element, handle it in the view or use a recognizer attached to that view. If you override raw touch methods in the controller for everything, the code usually becomes harder to reason about and easier to break.
Where Touch Events Actually Go
UIView inherits from UIResponder, which means it can receive touch callbacks such as:
- '
touchesBegan' - '
touchesMoved' - '
touchesEnded' - '
touchesCancelled'
The event is usually delivered to the view hit by the touch, not directly to the controller. A controller can also override these methods because it is in the responder chain, but that is usually not the first design choice for control-specific interaction.
Preferred Option: Use a Gesture Recognizer
For taps, pans, long presses, and similar common gestures, a recognizer is more maintainable than overriding raw touch methods.
This keeps the controller involved without forcing it to manually decode raw touch phases.
Use a Custom View for Low-Level Touch Logic
If the interaction is custom drawing, drag tracking, or fine-grained touch state, put that behavior in a UIView subclass.
And then use it from the controller:
This keeps the touch behavior where the touched UI actually lives.
When the Controller Should Handle Touches Directly
Direct controller overrides make sense when the whole screen is the interaction surface and there is no better view abstraction.
That works, but it is best reserved for screen-level behavior, not for emulating button or subview logic that should be localized.
Coordinate Systems Matter
A frequent source of confusion is using the wrong coordinate space. touch.location(in:) returns the touch position in whichever view you pass.
If gesture math looks wrong, check the view passed to location(in:) before debugging anything else.
Gesture Recognizers Versus Raw Touches
Use gesture recognizers when:
- the interaction matches a common gesture
- you want reusable, testable behavior
- you want the system to manage recognition state
Use raw touch methods when:
- you need fine-grained touch phase control
- you are building a drawing surface or custom interaction model
- gesture recognizers are too high-level for the task
That distinction keeps UIKit code more predictable.
Common Pitfalls
- Handling subview-specific touches in the controller instead of the view or a gesture recognizer.
- Forgetting to enable user interaction on the target view when needed.
- Using the wrong coordinate system in
location(in:). - Overriding touch methods without calling
superwhen the responder chain still matters. - Rebuilding tap or pan logic manually instead of using the built-in recognizers.
Summary
- Touches are usually delivered to the view that was hit, not directly to the controller.
- For common interactions, prefer gesture recognizers.
- For custom low-level touch behavior, use a
UIViewsubclass. - Controller-level raw touch overrides are best for screen-wide interaction, not fine-grained subview behavior.
- Most touch bugs are architectural or coordinate-space mistakes rather than UIKit mysteries.

