【发布时间】:2018-08-12 12:30:45
【问题描述】:
我在使用 Autolayout 和子类 UIButton 时遇到问题。在我的 UIButton 子类中,我重写 isEnabled 以在启用按钮时添加彩色动画。见下文。
// Within my subclassed button:
override var isEnabled:Bool {
didSet {
UIChanges.colorButton(buttonToFade: self)
}
}
// colorButton to be called from UIChanges:
static func colorButton(buttonToFade:UIView) {
let colorAnimation = CABasicAnimation(keyPath: "backgroundColor")
colorAnimation.duration = 1
colorAnimation.fromValue = UIColor.black.cgColor
colorAnimation.toValue = UIColor.white.cgColor
colorAnimation.repeatCount = 10
colorAnimation.autoreverses = true
buttonToFade.layer.add(colorAnimation, forKey: nil)
}
问题是,这个动画永远不会出现。
如果启用此 colorButton() 动画仅,如何将其添加到子类按钮?
我认为这与自动布局有关,因为如果我将该功能放在 layoutSubviews 中,它就可以正常工作。
编辑: 我向视图控制器添加了一个新按钮,该按钮手动将子类按钮更改为启用,@JD.答案工作正常(CABasicAnimation 触发)。但是,如果未按下该测试仪按钮,CABasicAnimation 不会触发。我试图调整的这个按钮主要在 AppDelegate 中启用 - 那么这可能是问题的原因吗?按钮设置为启用时未加载按钮框架?自动布局问题?
【问题讨论】:
标签: swift uibutton autolayout subclass cabasicanimation