如何控制UIView动画的速度?

时间:2022-11-06 00:16:02

I am implementing a rotating digit view like this: https://github.com/jonathantribouharet/JTNumberScrollAnimatedView

我正在实现这样的旋转数字视图:https://github.com/jonathantribouharet/JTNumberScrollAnimatedView

But, instead of scrolling from say 1 to 9, I want the animation to continue scrolling (1 to 9 then back to 1 to 9), until I manually stop it.

但是,我不想从1到9滚动,而是希望动画继续滚动(1到9然后再回到1到9),直到我手动停止它。

I implement this by adding UILabel subviews 1-9, vertically stacked, and add an extra label for number 1 at the bottom. When rotating to the bottom (showing the bottom label 1), in the animation completion closure, I set the transform back to origin, showing top label 1, and restart rotating animation.

我通过添加垂直堆叠的UILabel子视图1-9来实现这一点,并在底部为数字1添加额外的标签。当旋转到底部(显示底部标签1)时,在动画完成闭包中,我将变换设置回原点,显示顶部标签1,然后重新开始旋转动画。

This works fine, but since each rotation (1-9-1) is separate, the animation from 1-9 accelerates and then stop at 1, and pick up speed at the next iteration. This is not what I want, I want the rotation to maintain a constant speed between the iterations.

这样可以正常工作,但由于每个旋转(1-9-1)是分开的,因此1-9的动画加速然后停在1,并在下一次迭代时加速。这不是我想要的,我希望旋转在迭代之间保持恒定的速度。

How can I do this with UIView animation?

我怎么能用UIView动画做到这一点?

Here is the code:

这是代码:

 public func startAnimation(){
    UIView.setAnimationCurve(UIViewAnimationCurve.easeIn)
    UIView.animate(withDuration: TimeInterval(self.loopDuration), animations: { [unowned self] in
        self.container!.transform = CGAffineTransform(translationX: 0, y: CGFloat(-(self.endNumber-self.startNumber+1)*(self.size)))
    }) { [unowned self] (completed) in
        if self.isAnimationStopped {
            self.container!.transform = CGAffineTransform(translationX: 0, y: 0)
            UIView.setAnimationCurve(UIViewAnimationCurve.easeOut)
            UIView.animate(withDuration: TimeInterval(self.loopDuration), animations: {
                self.container!.transform = CGAffineTransform(translationX: 0, y: CGFloat(-self.targetY))
            })
        } else {
            self.container!.transform = CGAffineTransform(translationX: 0, y: 0)
            self.startAnimation()
        }
    }

}

1 个解决方案

#1


1  

By default UIView animation uses ease-in, ease-out animation timing, where the animation accelerates smoothly, runs, then decelerates to a stop.

默认情况下,UIView动画使用缓入,缓出动画计时,动画平滑加速,运行,然后减速停止。

You need to use a longer form of UIView.animate, animate(withDuration:delay:options:animations:completion:) that takes an options parameter, and specify a timing curve of .curveLinear.

您需要使用更长形式的UIView.animate,animate(withDuration:delay:options:animations:completion :)来获取options参数,并指定.curveLinear的时序曲线。

Just specify 0.0 for the delay and options: .curveLinear

只需为延迟和选项指定0.0:.curveLinear

#1


1  

By default UIView animation uses ease-in, ease-out animation timing, where the animation accelerates smoothly, runs, then decelerates to a stop.

默认情况下,UIView动画使用缓入,缓出动画计时,动画平滑加速,运行,然后减速停止。

You need to use a longer form of UIView.animate, animate(withDuration:delay:options:animations:completion:) that takes an options parameter, and specify a timing curve of .curveLinear.

您需要使用更长形式的UIView.animate,animate(withDuration:delay:options:animations:completion :)来获取options参数,并指定.curveLinear的时序曲线。

Just specify 0.0 for the delay and options: .curveLinear

只需为延迟和选项指定0.0:.curveLinear