<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/onTapGesture(count:perform:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE12onTapGesture5count7performQrSi_yyctF"
  },
  "title" : "onTapGesture(count:perform:)"
}
-->

# onTapGesture(count:perform:)

Adds an action to perform when this view recognizes a tap gesture.

```
nonisolated func onTapGesture(count: Int = 1, perform action: @escaping () -> Void) -> some View
```

## Parameters

`count`

The number of taps or clicks required to trigger the action
closure provided in `action`. Defaults to `1`.

`action`

The action to perform.

## Discussion

Use this method to perform the specified `action` when the user clicks
or taps on the view or container `count` times.

> Note: If you create a control that’s functionally equivalent
> to a ``doc://com.apple.SwiftUI/documentation/SwiftUI/Button``, use ``doc://com.apple.SwiftUI/documentation/SwiftUI/ButtonStyle`` to create a customized button
> instead.

In the example below, the color of the heart images changes to a random
color from the `colors` array whenever the user clicks or taps on the
view twice:

```
struct TapGestureExample: View {
    let colors: [Color] = [.gray, .red, .orange, .yellow,
                           .green, .blue, .purple, .pink]
    @State private var fgColor: Color = .gray

    var body: some View {
        Image(systemName: "heart.fill")
            .resizable()
            .frame(width: 200, height: 200)
            .foregroundColor(fgColor)
            .onTapGesture(count: 2) {
                fgColor = colors.randomElement()!
            }
    }
}
```

![A screenshot of a view of a heart.](images/com.apple.SwiftUI/SwiftUI-View-TapGesture@2x.png)

---

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)