Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 防止UIButton';s setTitle:forState:动画_Ios_Objective C_Swift_Animation_Uibutton - Fatal编程技术网

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按钮的标题

它可以工作,但标题中的文本会自动闪烁(动画设置为alpha
0
并返回)

我尝试使用
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类型。