UIButton 动画忽略 Swift 中的目标选择器

UIButton animation ignores target selector in Swift

我已将此动画代码添加到按下按钮时调用的函数末尾,但在动画发生时,它会忽略设置的目标选择器,直到动画完成。动画非常快,但我希望用户能够快速按下它。

        let transforms: CGAffineTransform = .identity

        mirroredButton.transform = transforms

        UIView.animate(withDuration: 0.05, animations: {
            mirroredButton.transform = transforms.scaledBy(x: 0.75, y: 0.75)
        },
        completion: { _ in
            UIView.animate(withDuration: 0.1) {
                mirroredButton.transform = transforms.scaledBy(x: 1.0, y: 1.0)
            }
        })

更新:

根据答案,我更新了我的动画代码,如下所示。两个动画调用都需要选项。第二个有一个 nil 完成处理程序。

        let transforms: CGAffineTransform = .identity

        mirroredButton.transform = transforms

        UIView.animate(withDuration: 0.05, delay: 0.0, options: .allowUserInteraction, animations: {
            mirroredButton.transform = transforms.scaledBy(x: 0.75, y: 0.75)
        },
        completion: { _ in
            UIView.animate(withDuration: 0.1, delay: 0.0, options: .allowUserInteraction, animations: {
                mirroredButton.transform = transforms.scaledBy(x: 1.0, y: 1.0)
            }, completion:nil)
        })

在视图动画期间禁用用户交互。如果用户在动画期间与视图交互很重要,您可以传入选项 .allowUserInteraction,如下所示:

UIView.animate(withDuration: 1.0, delay: 0.0, options: .allowUserInteraction, animations: {
    //animate here
})