Skip to main content

How to read tap and double-tap gestures

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to read tap and double-tap gestures 관련

SwiftUI by Example

Back to Home

How to read tap and double-tap gestures | SwiftUI by Example

How to read tap and double-tap gestures

Updated for Xcode 15

Any SwiftUI view can have tap actions attached, and you can specify how many taps should be received before the action is triggered.

For example, this creates a text view that will print a message when tapped:

Text("Tap me!")
    .onTapGesture {
        print("Tapped!")
    }

Download this as an Xcode project

And this creates an image view that will print a message when double tapped:

Image("singapore")
    .onTapGesture(count: 2) {
        print("Double tapped!")
    }

Download this as an Xcode project

Similar solutions…
How to detect the location of a tap inside a view | SwiftUI by Example

How to detect the location of a tap inside a view
How to make two gestures recognize at the same time using simultaneousGesture() | SwiftUI by Example

How to make two gestures recognize at the same time using simultaneousGesture()
How to detect shake gestures | SwiftUI by Example

How to detect shake gestures
How to stop system gestures from interfering with your own | SwiftUI by Example

How to stop system gestures from interfering with your own
All SwiftUI property wrappers explained and compared | SwiftUI by Example

All SwiftUI property wrappers explained and compared