2

I'm trying to implement a feature to an app I'm working on so that when a user taps a tab twice, it will automatically send the user back to the tab's initial view.

Suppose that I want the following "Devices" tab button to reload the view on a double tap:

enter image description here

This is the code I've been trying to use to solve this issue:

Tab View {
         DevicesScreen()
             .tabItem {
                 Image(systemName: "tv")
                 Text("Devices")
             }.onTapGesture(count: 2) {
                 DevicesScreen()
         }
}.font(.headline)

However, the result of the onTapGesture won't switch the view, therefore I wanted to ask whether there is another solution to the problem.

Thanks in advance.

4

1 回答 1

1

尝试这个:

struct ContentView: View {
    
    @State var selectedTab: Tab = .home
    
    var body: some View {
        TabView(selection: $selectedTab) {
            Text("Home Screen")
                .tabItem { Text("Home") }
                .tag(Tab.home)
            Text("More Screen")
                .tabItem { Text("More") }
                .tag(Tab.more)
            
            Text("bla Screen")
                .tabItem { Text("bla") }
                .tag(Tab.bla)
            
        }.onTapGesture(count: 2) {
            if self.selectedTab == .more {
                self.selectedTab = .home
            }
        }
    }
}

extension ContentView {
    enum Tab: Hashable {
        case home
        case more
        case bla
    }
}
于 2020-09-18T11:05:08.217 回答