Div
Div

Reputation: 1603

How to implement Haptic/3D Touch in SwiftUI?

How can I detect the force of a touch gesture in SwiftUI?

I can already do the following:

    view.onTapGesture { ... }

But how do I fire a different closure based on touch force, or wrap a UIKit view to achieve this?

Upvotes: 2

Views: 3448

Answers (1)

George Valkov
George Valkov

Reputation: 1459

You can use .contextMenu to display a list of actions for a specific element.

Text("Press hard or hold for context menu")
.contextMenu
{
    Button(action: { print("Action 1 triggered") }, label:
    {
        Text("Action 1")
    })
    
    Button(action: { print("action 2 triggered") }, label:
    {
        Text("Action 2")
    })
}

Upvotes: 8

Related Questions