用Swift创建具有”ease in”和”ease out”效果的无限旋转动画?

huangapple go评论60阅读模式
英文:

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: { () -&gt; Void in
        self.imgViewRoulette.transform = self.imgViewRoulette.transform.rotated(by: .pi / 2)
    }) { (finished) -&gt; Void in
        if iteration &lt; count {
            if finished {
                self.spin(iteration + 1, count: count)
            }
        }
    }
}

This can be called with something like:

someObject.spin(count: 8)

huangapple
  • 本文由 发表于 2023年3月7日 13:27:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定