UIButton过渡

问题描述 投票:2回答:2

我有一组六个按钮 - 当按下一个按钮(以编程方式)时,我希望它点亮然后淡出。所以我创建了一个名为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()
        }
}
swift uibutton transition
2个回答
1
投票

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

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

0
投票

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

你可以尝试这样的事情:

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

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

lightUpAndFadeOut(button: sender as! UIButton)

正确?

© www.soinside.com 2019 - 2024. All rights reserved.