我正在使用 SwiftUI 构建 Simon Says 应用程序,在构建它时我遇到了一个错误。 问题出在我在下面输入的一个特定函数上。 此函数设置 Simon Says 按钮的 alpha(只是按下按钮的简单动画)并将它们设置回 0.5。我希望它在每个 View 上一次运行一个,因为到目前为止动画同时在所有按钮上运行。
我们将不胜感激!
for index in settings.guessArray {
wait(time: 2.0) {
settings.alphas[index] = 1.0
wait(time: 0.3) {
settings.alphas[index] = 0.5
}
}
}
最佳答案
也许这样的事情会奏效......
等待是异步执行的,因此增加每个按钮的初始等待时间以及时隔开它们:
var offset = 0.0
for index in settings.guessArray {
wait(time: 2.0 + offset) {
settings.alphas[index] = 1.0
wait(time: 0.3) {
settings.alphas[index] = 0.5
}
}
// increase this value to increase the spacing between the buttons
// lighting up
offset += 0.3
}
//if you're using this function on multiple SwiftUI views at the same time
//consider placing the offset variable in @EnvironmentObject.
关于swift - 特定功能同时在所有 View 上运行。我怎样才能让它一个接一个地运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56824121/