gpt4 book ai didi

ios - iOS 14.0 Beta 中反复调用 DispatchQueue.main.asyncAfter

转载 作者:行者123 更新时间:2023-12-01 19:31:13 26 4
gpt4 key购买 nike

下面的延迟代码在 iOS 14.0 以下运行良好,但在 iOS 14.0 模拟器中,它每 2 秒重复调用一次。有什么我错过的吗?

  DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { // Change `2.0` to the desired number of seconds.
// Code you want to be delayed
}
在这里,我添加了完整的代码:
    struct ContentView: View {

@State var show = false

var body: some View {
NavigationView{

VStack{
NavigationLink(destination: Text("New View"), isActive: $show, label: {
Image("main_logo").renderingMode(.original).frame(width: 100, height: 100)
})
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self.show.toggle()
}
}
} .navigationBarHidden(true)
.navigationBarTitle(Text("Home"))
.edgesIgnoringSafeArea([.top, .bottom])



}.preferredColorScheme(.dark) // white tint on status bar

}
}
我在两秒后从这里调用新 View ,新 View 在两秒后关闭并返回到上面重复的 View 。
这就是我在控制台输出中得到的
[UIContextMenuInteraction] Attempting -[UIContextMenuInteraction dismissMenu], when not in an active state. This is a client error most often caused by calling dismiss more than once during a given lifecycle. (<_UIVariableGestureContextMenuInteraction: 0x60000121a920>)
我正在使用
Mac OS - Big Sur Beta 和 Xcode 12 Beta

最佳答案

.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self.show.toggle() // << this !!
}
}
.. self.show.toggle()使 body重建和 NavigationLink重新创建(因为它取决于 show ,因此 .onAppear 再次调用(因为它是新链接),您会看到 - 循环往复。
所以 DispatchQueue.main.asyncAfter没有错.
可能您打算将该 .onAppear 修饰符附加到 Root View ,即。大概 NavigationView .
更新:以下测试在 Xcode 12/iOS 14 上工作
struct ContentView: View {

@State var show = false

var body: some View {
NavigationView{

VStack{
NavigationLink(destination: Text("New View"), isActive: $show, label: {
Image("main_logo").renderingMode(.original).frame(width: 100, height: 100)
})
} .navigationBarHidden(true)
.navigationBarTitle(Text("Home"))
.edgesIgnoringSafeArea([.top, .bottom])
}.preferredColorScheme(.dark) // white tint on status bar
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self.show.toggle()
}
}
}
}

关于ios - iOS 14.0 Beta 中反复调用 DispatchQueue.main.asyncAfter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62737252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com