如何仅在长按手势SwiftUI上打开菜单

时间:2020-10-09 10:09:25

标签: swift swiftui

有没有一种方法可以在SwiftUI中长按手势显示上下文菜单?

我有这样的代码:

Menu() {
    Button("Order Now", action: {})
    Button("Adjust Order", action: {})
    Button("Cancel", action: {})
} label : {
    Text("HELLO")
        .onLongPressGesture {
            // Show menu when this triggers
        }
}

我也在SwiftUI中使用菜单,也不知道如何长按显示。

我的代码如下:

@UtilityClass
public class LocationMapper {

    private static final ModelMapper MAPPER = new ModelMapper();

    public LocationDTO toDTO(Location location) {
        return MAPPER.map(location, LocationDTO.class);
    }

    public Location toLocation(LocationDTO locationDTO) {
        return MAPPER.map(locationDTO, Location.class);
    }
}

1 个答案:

答案 0 :(得分:1)

这是一个简单的演示(为清楚起见,将menuItems提取到body之外):

struct ContentView: View {
    var body: some View {
        VStack {
            Text("On long press")
                .contextMenu {
                    menuItems
                }
            Menu("On tap") {
                menuItems
            }
        }
    }
    
    var menuItems: some View {
        Group {
            Button("Action 1", action: {})
            Button("Action 2", action: {})
            Button("Action 3", action: {})
        }
    }
}