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

标签 swift uiview uibutton transform uianimation

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

        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)
        })

最佳答案

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

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

关于swift - UIButton 动画忽略 Swift 中的目标选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56603166/

相关文章:

swift - 如何用字符替换特定范围的字符串?

ios - 具有 Objective-C 和快速代码的应用程序在 Release模式下启动时崩溃, Debug模式正常

swift - CIFilter CISmoothLinearGradient 在 Swift 中传递颜色时崩溃

ios - super.init 的 Circle Layer 错误 - Swift

ios - UIButton不会根据内部文本的长度垂直增长

使用 avspeechsynthesizer 快速代码将语音转换为文本

iphone - UIView 触摸处理的奇怪问题

ios - 如何使我的图像完全适合 UIView

ios - 动画按钮图像时 UIButton 文本不可见

iOS:UIButton 自动布局大小为零