Skip to content
tezvyn:
Menu

Creating Custom Gestures by Subclassing UIGestureRecognizer

Source: developer.apple.comMediumHow cards are made

Creating Custom Gestures by Subclassing UIGestureRecognizer

Subclassing UIGestureRecognizer lets you build a custom state machine for touch events. Use it for unique interactions like a rotary dial. The footgun is state management: you must explicitly update the gesture's state or it will never be recognized.

Why it exists

iOS provides a rich set of standard gestures like taps and swipes, but some applications require unique interactions not covered by the defaults. Subclassing UIGestureRecognizer was created to provide a formal, reusable way to define new gestures, encapsulating complex touch-parsing logic instead of scattering it inside a view controller.

The mental model

Think of a custom gesture recognizer as a small, focused state machine. It observes a raw stream of touch data, and your job is to write the rules that determine if that stream constitutes your specific gesture. You are essentially teaching the system a new pattern of touch interaction by defining what sequence of touches is significant.

How it works

You create a subclass of UIGestureRecognizer and override the core touch-handling methods: touchesBegan, touchesMoved, touchesEnded, and touchesCancelled. Within these methods, you inspect the UITouch objects to track finger movement, timing, and position. Based on your custom logic, you manually update the recognizer's state property. For a gesture to be successful, you must transition its state from the initial .possible to .began (when the gesture starts), then potentially .changed (as it continues), and finally to .ended. If the touch sequence doesn't match your criteria, you must set the state to .failed so the system can let other recognizers try.

When to use it

Subclass when you need a novel, reusable interaction that is core to your app's identity. This is ideal for things like a circular dial gesture, a specific drawing pattern to trigger an action (like drawing a checkmark), or a multi-finger "chord" to open a special menu. It keeps your code clean and your gesture logic portable.

When not to use it

Avoid subclassing for simple configurations of existing gestures. If you just need a two-finger tap, configure a standard UITapGestureRecognizer's numberOfTouchesRequired property. Subclassing is overkill if a built-in recognizer can be configured to do the job. It's also generally not for one-off, simple touch handling that is tightly coupled to a single view and will never be reused.

One canonical example

Creating a gesture to detect a small circular motion. In touchesBegan, you would store the initial touch point as the circle's center. In touchesMoved, you would calculate the distance and angle of the current touch point relative to the center. If the touch moves around the center while maintaining a relatively constant radius, you could set the state to .began and then .changed. If the finger is lifted after completing a near-full circle, you set the state to .ended. If the finger moves too far from the center or strays wildly, you set the state to .failed.

Interview question

What is the primary responsibility of a developer when implementing the touch-handling methods (e.g., touchesMoved) in a custom UIGestureRecognizer subclass?

  • a.Manually updating the recognizer's state property based on the custom gesture logic.Correct
  • b.Ensuring that the gesture recognizer is attached to only one view at a time.
  • c.Calculating the velocity and direction of the touches for system-level processing.
  • d.Calling the superclass's implementation to ensure default behavior is preserved.
Why?

The card explicitly states that developers 'must explicitly update the gesture's state' and 'manually update the recognizer's state property' based on custom logic. This state management is crucial for the gesture to be recognized. Other options describe actions that are either not the primary responsibility, incorrect, or handled by the system.

Just read this? Test yourself on what you have been reading.

Read the original → developer.apple.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles