按下按钮八秒后导航

发布于 2025-01-17 01:46:57 字数 1249 浏览 1 评论 0原文

这是我的按钮

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

,这个按钮同时执行功能和搜索,由于搜索需要时间,所以我看不到列表,我怎样才能执行该功能,然后在 8 秒后进行导航?谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里°也失望 2025-01-24 01:46:57

根据信息,您希望在 8 秒后切换到新视图。这段代码应该适合你。

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

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")
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文