<!--
{
  "availability" : [
    "macOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/NSGestureRecognizerRepresentable",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI32NSGestureRecognizerRepresentableP"
  },
  "title" : "NSGestureRecognizerRepresentable"
}
-->

# NSGestureRecognizerRepresentable

A wrapper for an `NSGestureRecognizer` that you use to integrate that
gesture recognizer into your SwiftUI hierarchy.

```
@MainActor @preconcurrency protocol NSGestureRecognizerRepresentable
```

## Overview

Use an [`NSGestureRecognizerRepresentable`](/documentation/SwiftUI/NSGestureRecognizerRepresentable) instance to create and manage an
<doc://com.apple.documentation/documentation/AppKit/NSGestureRecognizer>
object in your SwiftUI interface.

To add your gesture recognizer to a SwiftUI view, create an instance of
[`NSGestureRecognizerRepresentable`](/documentation/SwiftUI/NSGestureRecognizerRepresentable) and use the [`gesture(_:)`](/documentation/SwiftUI/View/gesture(_:))
modifier to attach it. The system calls the methods of your representable
instance at appropriate times to create and update the gesture recognizer.

The following example shows the inclusion of a custom `MyGestureRecognizer`
structure in the view hierarchy.

```
struct ContentView: View {
   var body: some View {
      VStack {
         Image("Mountain")
             .gesture(MyGestureRecognizer())
      }
   }
}
```

[`NSGestureRecognizerRepresentable`](/documentation/SwiftUI/NSGestureRecognizerRepresentable) types can use the environment and have
data dependencies, similar to views and view representables. The system will
call the appropriate methods on your instance to propagate the latest data.

## Handling Gesture Recognizer Actions

SwiftUI automatically installs a target to handle the gesture recognizer’s
action on your behalf. Implement the `handleNSGestureRecognizerAction`
method to react to the gesture recognizing its action.

You can optionally include a coordinator object to forward delegate messages
from your gesture recognizer or add additional targets.

## Coordinate Space Conversions

The gesture recognizer you create may not be attached to an `NSView` in
the hierarchy, or it may return a view with different geometry than your
SwiftUI view.

To handle this, use the converter on the context to perform coordinate space
conversions from the global coordinate space. You can pass the gesture
recognizer and a SwiftUI coordinate space to the converter to retrieve a
final converted point. Passing the [`local`](/documentation/SwiftUI/CoordinateSpaceProtocol/local)
coordinate space (the default) will provide the point in the SwiftUI view
the gesture recognizer is attached to.

---

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)