英文:
Infinite spin animation with "ease in" and "ease out" in Swift?
问题
我想创建一个“轮盘”(一个简单的UIImageView,带有适当的图像),并旋转它。
有类似的问题,但它们只描述了如何通过使用多次递归动画调用以线性速度旋转。但在我的情况下,我需要在旋转开始时使用easeIn动画,在停止时使用easeOut动画。因此,这些主题中没有一个答案有帮助。
如何解决这个问题?
代码示例
func spin() {
    UIView.animate(withDuration: 0.3, delay: 0, options: UIView.AnimationOptions.curveLinear, animations: { () -> Void in
        self.imgViewRoulette.transform = self.imgViewRoulette.transform.rotated(by: .pi / 2)
    }) { (finished) -> Void in
        if finished {
            self.spin()
        }
    }
}
英文:
I want to create a "roulette" (a simple UIImageView with appropriate image) and spin it.
There are similar questions but they describe just how to spin with linear speed by using multiple recursive animation calls. But in my case I need to use easeIn animation on start of spinning and easeOut on stop. So no one answer from those topics help.
How to resolve this issue?
Code Example
func spin() {
        UIView.animate(withDuration: 0.3, delay: 0, options: UIView.AnimationOptions.curveLinear, animations: { () -> Void in
                self.imgViewRoulette.transform = self.imgViewRoulette.transform.rotated(by: .pi / 2)
            }) { (finished) -> Void in
                if finished {
                    self.spin()
                }
            }
    }
答案1
得分: 0
以下是翻译好的代码部分:
func spin(_ iteration: Int = 0, count: Int) {
    let options: UIView.AnimationOptions
    switch iteration {
    case 0:
        options = .curveEaseIn
    case count:
        options = .curveEaseOut
    default:
        options = .curveLinear
    }
    UIView.animate(withDuration: 0.3, delay: 0, options: options, animations: { () -> Void in
        self.imgViewRoulette.transform = self.imgViewRoulette.transform.rotated(by: .pi / 2)
    }) { (finished) -> Void in
        if iteration < count {
            if finished {
                self.spin(iteration + 1, count: count)
            }
        }
    }
}
This can be called with something like:
someObject.spin(count: 8)
英文:
The following update to your spin function will start with an easeIn for a 1/4 turn, then do a series of linear 1/4 turns, and then end on an easeOut 1/4 turn.
func spin(_ iteration: Int = 0, count: Int) {
    let options: UIView.AnimationOptions
    switch iteration {
    case 0:
        options = .curveEaseIn
    case count:
        options = .curveEaseOut
    default:
        options = .curveLinear
    }
    UIView.animate(withDuration: 0.3, delay: 0, options: options, animations: { () -> Void in
        self.imgViewRoulette.transform = self.imgViewRoulette.transform.rotated(by: .pi / 2)
    }) { (finished) -> Void in
        if iteration < count {
            if finished {
                self.spin(iteration + 1, count: count)
            }
        }
    }
}
This can be called with something like:
someObject.spin(count: 8)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论