swift - 延迟循环调用动画流程
问题描述
这是我的代码的一部分,我试图延迟一个名为的函数,该函数dropText
从屏幕顶部删除一个名称。我尝试使用延迟功能,但它会延迟然后立即将它们全部丢弃。我错过了什么,或者这种方法完全错误?提前致谢:
func delay(_ delay:Double, closure:@escaping ()->()) {
DispatchQueue.main.asyncAfter(
deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure)
}
//New group choose method
func groupChoose()
{
//loop through the players
for x in 0...players - 1{
//drop the name in from the top of the screen
delay(2.0) {
self.dropText(playing[x])
}
}
解决方案
这个问题是因为你同时延迟了所有这些!您应该尝试为每个分配不同的延迟时间:
for x in 1...players {
//drop the name in from the top of the screen
delay(2.0 * x) {
self.dropText(playing[x-1])
}
重构
尽量不要按索引调用数组元素:
for playing in playing.enumerated() {
// drop the name in from the top of the screen
let player = playing.offset + 1
delay(2.0 * player) {
self.dropText(playing.element)
}
推荐阅读
- python - 如何用破折号打印日期?
- python - 对 django rest 框架使用范围过滤器
- spring-boot - 如何将drl文件的Cosequence中的值返回给java
- prometheus - Grafana + Prometheus 如何在查询指标中使用正则表达式?
- javascript - 为每个新状态创建新段落,而不是更新我的有状态组件
- javascript - 如何延迟 html/css 加载直到项目在视口中
- node.js - Angular 8 错误:'"node --max-old-space-size=10240"' 未被识别为内部或外部命令
- python - python项目中的Liquibase集成
- python - Django:检查object.filter列表中是否存在值
- mysql - 在nodejs中访问私有ec2实例中的MySQL服务器