UIButtons are not responding during animation - iO

2019-02-20 01:11发布

问题:

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.

I found some solutions for Objective-C using:

UIViewAnimationOptionAllowUserInteraction

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

回答1:

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