Home > Back-end >  navigate after eight seconds from pressing a button
navigate after eight seconds from pressing a button

Time:03-28

this is my button

   Button(action:  {
                                    SearchSomeone()
                                },label:  {
                                    NavigationLink(destination: mySearchList()){
                                    HStack(alignment: .center) {
                                        Text("Search")
                                            .font(.system(size: 17))
                                            .fontWeight(.bold)
                                            .foregroundColor(.white)
                                            .frame(minWidth: 0, maxWidth: .infinity)
                                            .padding()
                                            .background(
                                                RoundedRectangle(cornerRadius: 25)
                                                    .fill(Color("Color"))
                                                    .shadow(color: .gray, radius: 2, x: 0, y: 2)
                                                
                                            )
                                        
                                    }

and this button does the function and search together at the same time and since search would take time so I won't see the list, how can I do the function and then after 8 seconds I do the navigation after it ? thank you

CodePudding user response:

According to the information, you'd like to switch to a new view after 8 seconds. This code should work for you.

import SwiftUI

struct ContentView: View {

  //Variable to see if view should change or not
  @State var viewIsShowing = false

  var body: some View {
    //Detecting if variable is false
    if viewIsShowing == false {
    //Showing a button that sets the variable to true
    Button(action: {
      //Makes it wait 8 seconds before making the variable true
      DispatchQueue.main.asyncAfter(deadline: .now()   8.0) {
        viewIsShowing = true
      }
    }) {
      //Button text
      Text("Search")
                 .font(.system(size: 17))
                 .fontWeight(.bold)
                 .frame(minWidth: 0, maxWidth: .infinity)
                 .padding()
                 .background(
                     RoundedRectangle(cornerRadius: 25)
                         .fill(Color("Color"))
                         .shadow(color: .gray, radius: 2, x: 0, y: 2)
                  )
      }
    } else {
      //If the variable equals false, go here
      View2()
    }
  }
}

//Your other view you want to go to
struct View2: View {
  var body: some View {
    Text("abc")
  }
}
  • Related