立即 UIButton 旋转变换,带动画比例

Immediate UIButton rotation transform, with animated scale

我想为 UIButton 的缩放设置动画,并在完成后再次为缩放设置动画,但让它在没有动画的情况下旋转。我试图将旋转变换放在没有持续时间的动画调用中,但不知何故它仍然使它成为缩放动画的一部分或替换缩放动画。

为了更清楚地演示结果,我将比例的动画延长了一秒。

let transforms: CGAffineTransform = .identity

button.transform = transforms

UIView.animate(withDuration: 1.05, delay: 0.0, options: .allowUserInteraction, animations: {
    button.transform = transforms.scaledBy(x: 0.75, y: 0.75)
}, completion: { _ in

        button.transform = CGAffineTransform(rotationAngle: 90 * (.pi/180)) // I want this to happen immediately, without animation

        UIView.animate(withDuration: 1.1, delay: 0.0, options: .allowUserInteraction, animations: {
            button.transform = transforms.scaledBy(x: 1.0, y: 1.0)
        }, completion:nil)

})

您将轮换 transform 设置为立即发生 - 它确实发生了!但是然后你立即开始再次动画它,相对于你的 transforms 变量 - 将其当前 transform 恢复为该变量中的那个。

与其使用变量来跟踪按钮的 transform,不如参考其当前的 transform,如下所示:

button.transform = .identity
UIView.animate(withDuration: 1.05, delay: 0.0, options: .allowUserInteraction, animations: {
    button.transform = button.transform.scaledBy(x: 0.75, y: 0.75)
}, completion: { _ in
        button.transform = CGAffineTransform(rotationAngle: 90 * (.pi/180)) // I want this to happen immediately, without animation
        UIView.animate(withDuration: 1.1, delay: 0.0, options: .allowUserInteraction, animations: {
            button.transform = button.transform.scaledBy(x: 1.0, y: 1.0)
        }, completion:nil)

})