首页 > 解决方案 > 有没有一种方法可以在每次 for 循环重复时返回一些东西而无需退出 Swift 中的循环?

问题描述

我希望在平移一张图像后有很多图像具有动量,为了使这最简单,我将使用某种 for 循环为 UIView.animate 提供图像的动画数据,但如果我返回一个数据图像的工作 for 循环结束。我唯一的另一个想法是有一个数组来存储这些值并返回它,但我不知道存储这些值的变量类型。抱歉,我对 Swift 很陌生,老实说,我不知道这些东西中的一半叫什么,但希望通过代码你能理解我想要做什么。

UIView.animate(withDuration: TimeInterval(vectorToFinalPointLength/1800) ,delay: 0, options: .curveEaseOut, animations: {
            for (i,item) in self.ClosetItems.enumerated() {
                return item.center.x = finalPointCloset[i]
            }
        })

动画:想要多个语句,但由于返回结束了整个 for 循环,它只获得数组中的 30 个语句之一。谢谢您的帮助!

标签: swift

解决方案


您可以使用 forEach:

UIView.animate(
    withDuration: TimeInterval(vectorToFinalPointLength / 1800),
    delay: 0, 
    options: .curveEaseOut,
    animations: {
        self.ClosetItems.enumerated().forEach { i, item in
            item.center.x = finalPointCloset[i]
        }
    }
)

推荐阅读