UIView touch event in controller
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Touch handling in iOS can happen in a view subclass, in a view controller, or through a gesture recognizer. The right choice depends on what you are trying to detect. If you just need tap-like interaction in a controller, a gesture recognizer is usually cleaner than overriding raw touch methods.
Raw touchesBegan, touchesMoved, and related methods still matter when you need fine-grained touch tracking. The important detail is understanding where those events actually arrive in the responder chain.
Where Touch Events Should Live
UIView inherits from UIResponder, so a view can receive low-level touch callbacks directly. A UIViewController also participates in the responder system, but the controller is not usually the first place touch events land. The touched view receives them first.
That means:
- use a custom
UIViewsubclass when the view itself owns the interaction, - use a gesture recognizer when the controller only needs high-level gestures,
- override controller touch methods only when you specifically want controller-level raw touch handling.
For most apps, gesture recognizers are the best default.
Preferred Approach: Gesture Recognizer in the Controller
If your controller wants to react when the user taps a view, add a UITapGestureRecognizer:
This keeps the touch interpretation in UIKit's gesture system instead of forcing you to manually inspect UITouch objects.
When a Custom View Is Better
If the interaction belongs to the view itself, such as drawing, dragging, or custom hit-testing, override touch methods in a UIView subclass:
This is the right place for raw touch state because the view is the thing being interacted with.
Controller-Level Raw Touches
You can override touch methods in a UIViewController, but it is usually a specialized choice:
This works only when the responder chain reaches the controller. If a child view handles the touch or a gesture recognizer consumes it, the controller may not see what you expect. That is why controller-level raw touch handling is often surprising.
Common Pitfalls
- Overriding raw touch methods in the controller when a gesture recognizer would be simpler and more reliable.
- Expecting the controller to receive every touch before the view does. The touched view usually gets first chance.
- Forgetting
isUserInteractionEnabledon custom views that should respond to gestures. - Putting heavy work inside touch callbacks, which can make scrolling and animations feel laggy.
- Handling only
touchesBeganand ignoring moved, ended, or cancelled states when the interaction needs full tracking.
Summary
- Gesture recognizers are usually the cleanest way to handle taps and common gestures in a controller.
- Custom
UIViewsubclasses are the right place for raw touch tracking owned by the view itself. - Controller-level touch overrides are possible, but they are not the best default.
- The responder chain determines where touch events actually arrive.
- Choose the highest-level API that matches the interaction you need.
Related reading
- UIView touch event in controller
- UIView with rounded corners and drop shadow?
- UIViewContentModeScaleAspectFill not clipping
- UIViewController viewDidLoad vs. viewWillAppear What is the proper division of labor?
- UIView's border color in Interface builder doesn't work?
- UIWebView background is set to Clear Color, but it is not transparent
- UIWebView open links in Safari
- Unable to create Android Virtual Device
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.