UIButton 过渡

UIButton transition

我有一组六个按钮 - 当按下一个按钮(以编程方式)时,我希望它亮起然后淡出。所以我创建了一个名为 lightUp(button: UIButton) 的函数。我还为每个按钮设置了两个图像——点亮和未点亮。我可以从默认的未点亮状态变为点亮状态,但到目前为止,尝试先点亮按钮,然后再将其逐渐变回未点亮状态一直是个问题。在下面的代码中,按钮根本不亮。

func lightUp(button: UIButton){

    button.setImage(UIImage(named: padArrayOn[button.tag]), for: .normal)

        UIView.transition(with: button, duration: 0.4, options: .transitionCrossDissolve, animations: {
            button.setImage(UIImage(named: self.padArrayOff[button.tag]), for: .normal)
        }) { (bool) in
            self.playSequence()
        }
}

如果我没理解错你想要点亮按钮(也许改变背景)然后它应该淡出。

您可以尝试这样的操作:

func lightUpAndFadeOut(button: UIButton){
    UIView.animate(withDuration: 2) {
        button.backgroundColor = UIColor.red
    }
    UIView.animate(withDuration: 5) {
        button.alpha = 0
    }
}

每个按钮都会调用此函数,它应该如下所示:

lightUpAndFadeOut(button: sender as! UIButton)

正确吗?

设置两个转换,第二个可能在第一个的完成处理程序中?

 func lightUp(button: UIButton){
        UIView.transition(with: button, duration: 0.2, options: .transitionCrossDissolve, animations: {
            button.setImage(UIImage(named: padArrayOn[button.tag]), for: .normal)
            }, completion: {_ in
                UIView.transition(with: button, duration: 0.2, options: .transitionCrossDissolve, animations: {
                    button.setImage(UIImage(named: self.padArrayOff[button.tag]), for: .normal)
                    }, completion: { _ in
                        self.playSequence()
                    }
        )})
}