iOS 动画重复(未调用完成块)

iOS Animation Repeat (Completion Block Not Being Called)

我想让我的动画来回移动。例如,在这样的鱼缸模拟中,我试图让鱼来回移动。我试图让它无限长地工作,但是鱼只朝一个方向移动并且永远不会调用完成块。有人可以告诉我如何在鱼到达屏幕末尾时调用完成块或其他方法吗?

附件是我当前的代码:

UIView.animate(withDuration: fishAnimationDuration, delay: fishAnimationDelay, options: [.allowAnimatedContent, .repeat], animations: {
                fish.frame = CGRect(x: fishXPosEnd, y: fishYPos, width: fishWidth, height: fishHeight)
            }, completion: { (finished: Bool) in
                    fish.image = #imageLiteral(resourceName: "fishBack")
                    UIView.animate(withDuration: fishAnimationDuration, delay: fishAnimationDelay, options: [.allowAnimatedContent], animations: {
                        fish.frame = CGRect(x: fishXPos, y: fishYPos, width: fishWidth, height: fishHeight)
                   })
 })

repeat 在选项中时,不会调用完成块,因为重复动画永远不会完成。

在你的情况下,我会删除 repeat 选项。然后在第二个动画的完成处理程序中,调用包含这些动画块的方法,以便再次调用这对动画。