Swift - 如何在 for 循环中处理完成块?

巴拉苏布拉马尼亚

我想一个接一个地显示多个动画。gifArray有动画列表。我在 for 循环中循环这个数组。发生的事情是最后一个动画正在显示并完成。

只有当当前循环动画完成时,我才能继续每个循环?

for index in 0..<gifArray.count {
   self.makeAnimation(value: index)
}

func makeAnimation(value: Int){ 
    let anotherAnimationView = LOTAnimationView(name: gifNames[value])
    animationView = anotherAnimationView
    animationView?.play(completion: { finished in
        print("completed")
    })
    self.view.addSubview(animationView!)
}
拉什万

您可以将一个添加completionHandler到您的函数中,您可以在animation完成后调用该函数

func makeAnimation(value: Int, onCompletion: @escaping (LOTAnimationView) -> Void) {
    let anotherAnimationView = LOTAnimationView(name: gifNames[value])
    animationView = anotherAnimationView
    animationView?.play(completion: { finished in
        print("completed")
        onCompletion(animationView)
    })
}

并使用它使用以下内容:

makeAnimation(value: 1000000) { (animationView) in
    self.view.addSubview(animationView!)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章