【问题标题】:UIButtons are not responding during animation - iOS Swift 3UIButtons 在动画期间没有响应 - iOS Swift 3
【发布时间】:2017-05-03 15:52:08
【问题描述】:

我正在构建一个计算器应用程序,当我点击按钮时会出现一个简短的动画。问题是按钮在动画时没有响应,这让应用感觉迟钝。

我找到了一些针对 Objective-C 的解决方案:

UIViewAnimationOptionAllowUserInteraction

但 Swift 3 没有,我的代码如下所示:

func myButton () {

    sender.layer.backgroundColor = firstColor
    sender.setTitleColor(UIColor.white, for: UIControlState.normal)

    UIView.animate(withDuration: 0.5) {
        sender.layer.backgroundColor = secondColor
    }
}

【问题讨论】:

    标签: ios swift animation uibutton


    【解决方案1】:

    调用UIView的不同方法:

    func myButton () {
        sender.layer.backgroundColor = firstColor
        sender.setTitleColor(UIColor.white, for: UIControlState.normal)
    
        UIView.animate(withDuration: 0.5,
                       delay: 0.0,
                       options: .allowUserInteraction,
                       animations: {
                           sender.layer.backgroundColor = secondColor },  
                       completion: nil)
    }
    

    【讨论】:

    • Sasha 的回答是正确的(已投票)。为了解释,默认情况下,系统会在动画期间禁用用户交互。通过传入.allowUserInteraction 选项,您将覆盖该默认值。但是,您仍然无法单击在飞行途中从一个地方移动到另一个地方的按钮。真正发生的是,一旦动画开始,视图就会被放置在它的最终位置,并且动画是在顶部绘制的仅显示的“表示层”中完成的。如果您希望能够点击正在移动的东西,您必须编写自定义代码才能做到这一点。
    • @DuncanC 我们如何点击正在移动的东西?
    • @YunusT.,你不能,通常。您必须将轻击手势识别器附加到父视图,然后在表示层中对移动视图的层进行测试。如果您想要详细的答案,您应该发布另一个问题。
    猜你喜欢
    • 1970-01-01
    • 2017-05-24
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 2010-11-10
    相关资源
    最近更新 更多