简体   繁体   中英

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?

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")
    })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM