发生这种情况是因为您的动画与默认按钮动画冲突。
为了解决这个问题,你可以创建一个自定义子类的UIButton的:
import UIKit
import UIKit
class CustomButton: UIButton {
func animateButton(hex: String) {
// This block disables the default animations
UIButton.performWithoutAnimation {
// This is where you define your custom animation
UIView.transition(with: self, duration:3, options: UIViewAnimationOptions.transitionCrossDissolve, animations: {
self.backgroundColor? = UIColor(hexaString: "#" + hex) }, completion: nil)
// You must call layoutIfNeeded() at the end of the block to prevent layout problems
self.layoutIfNeeded()
}
}
}
然后调用这个函数:
signInButton.animateButton("ffffff") // Pass in your hex string
当然,一定要改变你的loginButton的类在故事板上从UIButton
到CustomButton
。
这种方法的缺点是在动画过程中文本仍然变暗。可能有一种方法可以覆盖这个(我无法快速确定如何),所以此解决方案的另一种方法是使用UILabel而不是UIButton并设置点击监听器。
或者您可能需要考虑的另一种方法是在UIButton后面放置一个customLabel,并在单击该按钮时在标签上执行动画。虽然这种方法看起来有些“黑客”,但好处是在标签动画时按钮不会被禁用,这意味着您可以注册快速顺序按钮按钮(尽管按钮的用途看起来像是登录用户,在这种情况下,这可能不需要你)。
谢谢!当我在我的笔记本电脑上时,我会对此进行测试。 –
完美的作品! –
欢迎伴侣:) –