变换动画不能重复使用



我有一个圆形视图buttonView我在UIView动画中应用CGAffineTransform,如下所示:

func animateButton() {
    UIView.animate(withDuration: 3, delay: 0.0, options: [.curveEaseInOut, .allowUserInteraction], animations: {() -> Void in
                        self.buttonView.transform = CGAffineTransform(scaleX: 1, y: 1)
                    }, completion: {(_ finished: Bool) -> Void in
                UIView.animate(withDuration: 3, delay: 3.0, options: [.curveEaseInOut, .allowUserInteraction], animations: {() -> Void in
                        self.buttonView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
                }, completion: {(_ finished: Bool) -> Void in
                        if canAnimateButton {
                               animateButton()
                        }
                })
}

这个动画在一个函数中,该函数被称为自身(递归)来构建无限循环。

然后我停止触摸视图的动画:

    buttonView.layer.removeAllAnimations()

当按钮动作结束时,我想通过调用相同的函数animateButton()重新启动动画。

我的问题:动画不起作用,转换会立即应用。

提前感谢您的帮助。

通过调用buttonView.layer.removeAllAnimations(),这不会停止递归循环,因为仍然会调用完成处理程序。

与其创建这个递归循环,不如考虑使用 CATransition,因为它可以让您对 UI 动画进行更有限的控制。

最新更新