gpt4 book ai didi

ios - 直接从突出显示的颜色切换颜色到另一种颜色

转载 作者:行者123 更新时间:2023-11-28 06:17:17 26 4
gpt4 key购买 nike

我在 mainStoryboard 上有一个 UIButton。它有白色 UIColor 和橙色高亮颜色。

我想在选择按钮后立即更改此按钮的颜色。

理想的结果

White(default) -> orange(highlighted) -> green(animated) -> white(default)

然而,使用以下代码,在颜色从橙色变为绿色之前,它会很快变为白色。

当前结果

White(default) -> orange(highlighted) -> White(default) -> green(animated) -> white(default)

如何将颜色直接从突出显示的橙色切换为绿色?

    UIView.animate(withDuration:0, animations: { () -> Void in
cell.buttons[index].backgroundColor = UIColor.green

}) { (Bool) -> Void in
UIView.animate(withDuration: 0.5, animations: { () -> Void in
cell.buttons[index].backgroundColor = UIColor.green

}, completion: { (Bool) -> Void in
UIView.animate(withDuration: 0, animations: { () -> Void in
cell.buttons[index].backgroundColor = UIColor.white
}, completion:nil)
})
}

最佳答案

您的动画代码看起来不错,但动画是两种状态之间的交易,它需要时间(持续时间)。所以尽量不要有持续时间为 0 秒的动画,因为在这种情况下,动画是无用的。

您的问题似乎在按钮监听器上出现了错误。您希望在单击按钮时立即将颜色更改为橙​​色,即 touchDown。然后你想在按钮一松开的时候就改变颜色,也就是touchUpInside

所以尝试一下,将此代码添加到您的 viewDidLoad

yourButton.addTarget(self, action:#selector(btnShowPasswordClickHoldDown), for: .touchDown)

yourButton.addTarget(self, action:#selector(btnShowPasswordClickRelease), for: .touchUpInside)

然后添加具有有效持续时间的动画

func btnShowPasswordClickHoldDown(){
UIView.animate(withDuration: 0.5, animations: { () -> Void in
self.yourButton.backgroundColor = UIColor.orange
}, completion:nil)
}

func btnShowPasswordClickRelease(){
UIView.animate(withDuration: 0.5, animations: { () -> Void in
self.yourButton.backgroundColor = UIColor.green

}, completion: { (Bool) -> Void in
UIView.animate(withDuration: 0.5, animations: { () -> Void in
self.yourButton.backgroundColor = UIColor.white
}, completion:nil)
})
}

关于ios - 直接从突出显示的颜色切换颜色到另一种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44760125/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com