【问题标题】:UIButton animation ignores target selector in SwiftUIButton 动画忽略 Swift 中的目标选择器
【发布时间】:2019-06-14 18:01:08
【问题描述】:

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

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

更新:

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

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

【问题讨论】:

  • 您希望目标选择器做什么?重新开始动画?
  • 是的,那很好。我只是不希望它取消在动画时触发目标选择器功能的能力。

标签: swift uiview uibutton transform uianimation


【解决方案1】:

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

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

【讨论】:

  • 谢谢,就是这样。在使用 nil 完成处理程序的第二个动画调用中使用 allowUserInteraction 选项会更好。更新了我的代码以显示这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
  • 2012-02-26
  • 1970-01-01
相关资源
最近更新 更多