<!--
{
  "documentType" : "article",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/implementing-a-custom-gesture-recognizer",
  "metadataVersion" : "0.1.0",
  "role" : "collectionGroup",
  "title" : "Implementing a custom gesture recognizer"
}
-->

# Implementing a custom gesture recognizer

Discover when and how to build your own gesture recognizers.

## Discussion

When the built-in UIKit gesture recognizers don’t provide the behavior you want, you can define custom gesture recognizers. UIKit defines highly configurable gesture recognizers to handle touch sequences for taps, long presses, pans, swipes, rotations, and pinches. For other touch sequences, or to handle gestures that involve button presses, you can define a custom gesture recognizer.

You might also use a custom gesture recognizer to simplify the event-handling code in your app. For example, the [Leveraging touch input for drawing apps](/documentation/UIKit/leveraging-touch-input-for-drawing-apps) sample uses a gesture recognizer to capture input and display it onscreen, as shown in the following image.

![A screenshot from an app that uses a custom gesture recognizer to allow a user to draw on the screen.](images/com.apple.uikit/implementing-a-custom-gesture-recognizer-1@2x.png)

To define a custom gesture recognizer, subclass [`UIGestureRecognizer`](/documentation/UIKit/UIGestureRecognizer) (or one of its subclasses). At the top of your source file, import the `UIGestureRecognizerSubclass.h` header file (for Objective-C) or the `UIKit.UIGestureRecognizerSubclass` module (for Swift), as shown in the following code. This file defines the methods and properties that you must override to implement your custom gesture recognizer.

```objc
#import <UIKit/UIKit.h>
#import "UIGestureRecognizerSubclass.h"
```

In your custom subclass, implement whatever methods you need to process events. For example, if your gesture consists of touch events, implement the [`touchesBegan(_:with:)`](/documentation/UIKit/UIResponder/touchesBegan(_:with:)), [`touchesMoved(_:with:)`](/documentation/UIKit/UIResponder/touchesMoved(_:with:)), [`touchesEnded(_:with:)`](/documentation/UIKit/UIResponder/touchesEnded(_:with:)), and [`touchesCancelled(_:with:)`](/documentation/UIKit/UIResponder/touchesCancelled(_:with:)) methods. Use incoming events to update the [`state`](/documentation/UIKit/UIGestureRecognizer/state-swift.property) property of your gesture recognizer. UIKit uses the gesture recognizer states to coordinate interactions with other objects in your interface.

## Topics

### Creating Custom Gesture Recognizers

[About the Gesture Recognizer State Machine](/documentation/UIKit/about-the-gesture-recognizer-state-machine)

Learn about the states and transitions of the state machine that underlies gesture recognizers.

[Implementing a discrete gesture recognizer](/documentation/UIKit/implementing-a-discrete-gesture-recognizer)

If your gesture involves a specific pattern of events, consider implementing a discrete gesture recognizer for it.

[Implementing a Continuous Gesture Recognizer](/documentation/UIKit/implementing-a-continuous-gesture-recognizer)

For gestures that do not easily match a specific pattern, or when you want to use a gesture recognizer to gather touch input, create a continuous gesture recognizer.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)