UIButtons 在动画期间没有响应 - iOS Swift 3 [英] UIButtons are not responding during animation - iOS Swift 3

查看:12
本文介绍了UIButtons 在动画期间没有响应 - iOS Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building a calculator app, when I tap on the buttons there is a short animation. The problem is the buttons do not respond while they are animating, which makes the app feel laggy.

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

I found some solutions for Objective-C using:

UIViewAnimationOptionAllowUserInteraction

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

but nothing for Swift 3, my code looks like this:

func myButton () {

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

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

推荐答案

调用UIView的不同方法:

Call different method of 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)
}

这篇关于UIButtons 在动画期间没有响应 - iOS Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆