我想为 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)
})
您将旋转 biến đổi
设置为立即发生 - 它确实发生了!但是随后您立即开始再次为其设置动画,关于您的 transforms
变量 - 将其当前的 biến đổi
恢复为该变量中的那个。
与其使用变量来跟踪按钮的biến đổi
,不如引用其当前的biến đổi
, như hình dưới đây:
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)
})
Tôi là một lập trình viên xuất sắc, rất giỏi!