特定功能 运行s 同时出现在所有视图上。我怎样才能使运行一个接一个?
Specific function runs at the same moment on all of the views. How can I make it run one after the other?
我正在使用 SwiftUI 构建 Simon Says 应用程序,在构建它时遇到了一个错误。
问题出在我在下面输入的一个特定函数上。
此函数设置 Simon Says 按钮的 alpha(只是按下按钮的简单动画)并将它们设置回 0.5。我希望在每个视图上一次 运行 一个,因为到目前为止所有按钮上的动画 运行 是同时出现的。
我们将不胜感激!
for index in settings.guessArray {
wait(time: 2.0) {
settings.alphas[index] = 1.0
wait(time: 0.3) {
settings.alphas[index] = 0.5
}
}
}
我在这里瞎了眼,但你用信号量实现了这一点。改编自以下代码:
let semaphore = DispatchSemaphore(value: 0)
for index in settings.guessArray {
semaphore.wait() // When animation on one button begins
wait(time: 2.0) {
settings.alphas[index] = 1.0
wait(time: 0.3) {
settings.alphas[index] = 0.5
semaphore.signal() // When animation on a button finishes
}
}
}
也许这样的事情会奏效...
等待是异步执行的,因此将每个按钮的初始等待时间增加到 space 及时结束:
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.
我正在使用 SwiftUI 构建 Simon Says 应用程序,在构建它时遇到了一个错误。 问题出在我在下面输入的一个特定函数上。 此函数设置 Simon Says 按钮的 alpha(只是按下按钮的简单动画)并将它们设置回 0.5。我希望在每个视图上一次 运行 一个,因为到目前为止所有按钮上的动画 运行 是同时出现的。
我们将不胜感激!
for index in settings.guessArray {
wait(time: 2.0) {
settings.alphas[index] = 1.0
wait(time: 0.3) {
settings.alphas[index] = 0.5
}
}
}
我在这里瞎了眼,但你用信号量实现了这一点。改编自以下代码:
let semaphore = DispatchSemaphore(value: 0)
for index in settings.guessArray {
semaphore.wait() // When animation on one button begins
wait(time: 2.0) {
settings.alphas[index] = 1.0
wait(time: 0.3) {
settings.alphas[index] = 0.5
semaphore.signal() // When animation on a button finishes
}
}
}
也许这样的事情会奏效...
等待是异步执行的,因此将每个按钮的初始等待时间增加到 space 及时结束:
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.