创建一个用于执行动画的等待队列
Create a waiting queue for executing an animation
我目前正在制作 swift 动画。此动画在不稳定的时间间隔内由另一个函数触发(连接到服务器)。动画需要 2 秒才能完成,但它有可能在完成之前被触发。
这就是为什么我正在考虑创建一个等待队列来存储触发事件,直到动画完成并可以重新启动。因此,一方面我必须锁定动画功能直到它再次准备就绪,另一方面我需要同时为传入事件进行某种存储。我已经在考虑调度组,但不知道如何使用它们。
如果我能向哪个方向解决这个问题,我会非常高兴。
触发函数:
private func subscribeToNewBLock() {
DispatchQueue.global(qos:.userInteractive).async {
watchForNewBlock() {result in
switch result {
case .Failure:
return
case .Success(let result):
//Animation function
self.moveBlocksDown(blockNumber: result)
//Recursive call to keep listening for new blocks
self.subscribeToNewBLock()
}
}
}
}
您可以尝试像下面的示例那样制作动画队列
var results = [Int]()
var isAnimating = false
private func subscribeToNewBLock() {
DispatchQueue.global(qos:.userInteractive).async {
watchForNewBlock() {result in
switch result {
case .Failure:
return
case .Success(let result):
//Call your UI operations in main thread
DispatchQueue.main.async {
self.results.append(result)
//Animation function
self.moveBlocksDown()
//Recursive call to keep listening for new blocks
self.subscribeToNewBLock()
}
}
}
}
}
private func moveBlocksDown() {
guard isAnimating == false && results.count > 0 else {
return
}
self.moveBlocksDown(blockNumber: results.first!)
}
private func moveBlocksDown(blockNumber:Int){
isAnimating = true
UIView.animate(withDuration: 2.0, animations: {
//Animation code goes here
}) { (completed) in
if completed{
//Add follwing code in place of animation completed(May be in completion handler)
self.isAnimating = false
self.results = self.results.filter{[=10=] != blockNumber} //Remove already animated blockNumber
self.moveBlocksDown() //Call moveBlocksDown function to check if anything pending in queue
}
}
}
我目前正在制作 swift 动画。此动画在不稳定的时间间隔内由另一个函数触发(连接到服务器)。动画需要 2 秒才能完成,但它有可能在完成之前被触发。 这就是为什么我正在考虑创建一个等待队列来存储触发事件,直到动画完成并可以重新启动。因此,一方面我必须锁定动画功能直到它再次准备就绪,另一方面我需要同时为传入事件进行某种存储。我已经在考虑调度组,但不知道如何使用它们。 如果我能向哪个方向解决这个问题,我会非常高兴。
触发函数:
private func subscribeToNewBLock() {
DispatchQueue.global(qos:.userInteractive).async {
watchForNewBlock() {result in
switch result {
case .Failure:
return
case .Success(let result):
//Animation function
self.moveBlocksDown(blockNumber: result)
//Recursive call to keep listening for new blocks
self.subscribeToNewBLock()
}
}
}
}
您可以尝试像下面的示例那样制作动画队列
var results = [Int]()
var isAnimating = false
private func subscribeToNewBLock() {
DispatchQueue.global(qos:.userInteractive).async {
watchForNewBlock() {result in
switch result {
case .Failure:
return
case .Success(let result):
//Call your UI operations in main thread
DispatchQueue.main.async {
self.results.append(result)
//Animation function
self.moveBlocksDown()
//Recursive call to keep listening for new blocks
self.subscribeToNewBLock()
}
}
}
}
}
private func moveBlocksDown() {
guard isAnimating == false && results.count > 0 else {
return
}
self.moveBlocksDown(blockNumber: results.first!)
}
private func moveBlocksDown(blockNumber:Int){
isAnimating = true
UIView.animate(withDuration: 2.0, animations: {
//Animation code goes here
}) { (completed) in
if completed{
//Add follwing code in place of animation completed(May be in completion handler)
self.isAnimating = false
self.results = self.results.filter{[=10=] != blockNumber} //Remove already animated blockNumber
self.moveBlocksDown() //Call moveBlocksDown function to check if anything pending in queue
}
}
}