Ios 防止UIButton';s setTitle:forState:动画
我正在使用Ios 防止UIButton';s setTitle:forState:动画,ios,objective-c,swift,animation,uibutton,Ios,Objective C,Swift,Animation,Uibutton,我正在使用NSTimer每秒更新ui按钮的标题 它可以工作,但标题中的文本会自动闪烁(动画设置为alpha0并返回) 我尝试使用button.layer.removeAllimations(),但没有运气,也没有例外,因此QuartzCore似乎链接正确 当前不工作的偏执狂代码: UIView.setAnimationsEnabled(false) UIView.performWithoutAnimation { button.setTitle(time, forState: .Nor
NSTimer
每秒更新ui按钮的标题
它可以工作,但标题中的文本会自动闪烁(动画设置为alpha0
并返回)
我尝试使用button.layer.removeAllimations()
,但没有运气,也没有例外,因此QuartzCore似乎链接正确
当前不工作的偏执狂代码:
UIView.setAnimationsEnabled(false)
UIView.performWithoutAnimation {
button.setTitle(time, forState: .Normal)
button.layer.removeAllAnimations()
}
UIView.setAnimationsEnabled(true)
您可以在封口内执行按钮更改:
UIView.performWithoutAnimation {
//Button changes
button.layoutIfNeeded()
}
希望这有帮助。确保您的按钮是“自定义”按钮,而不是“系统”按钮
如果是在故事板上创建的,则只需更改按钮类型。如果是以编程方式创建的,则应为:
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
为此,我已迅速进行了扩展:
extension UIButton {
func setTitleWithOutAnimation(title: String?) {
UIView.setAnimationsEnabled(false)
setTitle(title, forState: .Normal)
layoutIfNeeded()
UIView.setAnimationsEnabled(true)
}
}
我可以在iOS 8和iOS 9上使用UIButtonTypeSystem
您是否尝试在没有动画的情况下使用UIView
的性能来包装它(uu.actionswithoutimation:()->Void)
(iOS>=7)?或者SetAnimationEnabled(uEnabled:Bool)
?太糟糕了,结果是一样的。另外,在没有动画的情况下调用UIView.Perform会比较短{//按钮更改}您仍然需要调用layoutIfNeeded(),我发现这是正确的答案!设置setTitle调用后所需的layoutifn就是所需的全部内容。在UIView.performWithoutAnimation中包装它似乎没有任何作用。很好!但我不明白为什么一个系统按钮拒绝服从标准的UIKit命令。最新的Swift版本也适用于我。在IB中也使用UIButtonTypeSystem。谢谢!在Swift 3.1上测试,仍然有效!这将禁用整个应用程序的动画,只是为了不设置按钮的动画。。。它可以工作,但最好只使用自定义UIButton类型。