更新标题时如何防止UIButton闪烁

Fie*_*key 80 uibutton ios7

当我在UIButton上调用setTitle时,该按钮在iOS 7中闪烁.我尝试设置myButton.highlighted = NO,但这并没有阻止按钮闪烁.

[myButton setTitle:[[NSUserDefaults standardUserDefaults] stringForKey:@"elapsedLabelKey"] forState:UIControlStateNormal];

myButton.highlighted = NO;
Run Code Online (Sandbox Code Playgroud)

以下是我如何设置更新标题的计时器:

- (void)actionTimer {
    if (myTimer == nil) {

        myTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0
                        target: self
                        selector: @selector(showActivity)
                        userInfo: nil
                        repeats: YES];
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是实际更新标题的方法:

- (void)showActivity {

    NSString *sym = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol];

    if (pauseInterval == nil) {

        // Update clock
        seconds = [[NSDate date] timeIntervalSinceDate:startInterval] - breakTime;

        // Update total earned
        secRate = rate.value / 60 / 60;
        total = secRate * seconds;
        [totalLabel setTitle:[NSString stringWithFormat:@"%@%.4f",sym,total] forState:UIControlStateNormal];

        days = seconds / (60 * 60 * 24);
        seconds -= days * (60 * 60 * 24);
        int hours = seconds / (60 * 60);
        fhours = (float)seconds / (60.0 * 60.0);
        seconds -= hours * (60 * 60);
        int minutes = seconds / 60;
        seconds -= minutes * 60;

        // Update the timer clock
        [elapsed setTitle:[NSString stringWithFormat:@"%.2i:%.2i:%.2i:%.2i",days,hours,minutes,seconds] forState:UIControlStateNormal];
    }
}
Run Code Online (Sandbox Code Playgroud)

Qua*_*ong 187

将按钮类型设置为UIButtonTypeCustom,它将停止闪烁

  • 这应该是首选答案.最简单的解决方案.它正在发挥作用. (13认同)
  • 同意.我尝试了很多其他解决方案,这就是它所做的事情,一切都是独一无二的. (3认同)
  • 这应该是公认的答案。迄今为止最简单的解决方案。 (2认同)
  • 我认为这是一个非常好的解决方案,无需为此编写任何代码。 (2认同)

Cœu*_*œur 41

[UIView setAnimationsEnabled:NO]其他动画更好的方法是仅禁用特定的标题动画.

Objective-C的:

[UIView performWithoutAnimation:^{
  [myButton setTitle:text forState:UIControlStateNormal];
  [myButton layoutIfNeeded];
}];
Run Code Online (Sandbox Code Playgroud)

斯威夫特3:

UIView.performWithoutAnimation { 
    myButton.setTitle(text, for: .normal)
    myButton.layoutIfNeeded()
}
Run Code Online (Sandbox Code Playgroud)

  • 毫无疑问,这应该是公认的答案! (2认同)

小智 29

*请注意*

_button的 " buttonType "是"UIButtonTypeSystem"时,下面的代码无效:

[UIView setAnimationsEnabled:NO];
[_button setTitle:@"title" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
Run Code Online (Sandbox Code Playgroud)

_button的 " buttonType "为"UIButtonTypeCustom"时,上面的代码有效.


Mar*_*ius 18

请参阅如何在标题更改时停止不需要的UIButton动画的响应:

[UIView setAnimationsEnabled:NO];
[elapsed setTitle:[NSString stringWithFormat:@"%.2i:%.2i:%.2i:%.2i",days,hours,minutes,seconds] forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
Run Code Online (Sandbox Code Playgroud)

  • 即使使用此代码,它仍然会闪烁. (9认同)
  • @FierceMonkey尝试添加对`layoutIfNeeded`的调用:`[elapsed layoutIfNeeded];`.这消除了我的情况下的眨眼. (7认同)

小智 7

您只需为UIButton创建另一个功能,以便日后使用.

extension UIButton {
    func setTitleWithoutAnimation(_ title: String?, for controlState: UIControlState) {
        UIView.performWithoutAnimation {
            self.setTitle(title, for: controlState)
            self.layoutIfNeeded()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这就对了!

只需像以前一样设置标题,但替换setTitlesetTitleWithoutAnimation


Luc*_*nzo 6

默认的"setTitle"行为绝对是可恶的!

我的解决方案是:

[UIView setAnimationsEnabled:NO];
[_button layoutIfNeeded]; 
[_button setTitle:[NSString stringWithFormat:@"what" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
Run Code Online (Sandbox Code Playgroud)

另外,在storyboard中,在按钮的属性下,我取消选中:

  • 反转突出显示
  • 突出显示的调整图像

并检查:

  • 禁用调整图像

测试并在iOS 8上工作.

更新 - 斯威夫特

我建议你创建自定义按钮并覆盖setTitle方法.

class UnflashingButton: UIButton {

    override func setTitle(title: String?, forState state: UIControlState) {
        UIView.setAnimationsEnabled(false)
        self.layoutIfNeeded()
        super.setTitle(title, forState: state)
        UIView.setAnimationsEnabled(true)
    }

}
Run Code Online (Sandbox Code Playgroud)