如何在更新标题时防止UIButton闪烁
当我在UIButton上调用setTitle时,button会在iOS 7中闪烁。我尝试设置myButton.highlighted = NO,但是这并没有阻止button闪烁。
[myButton setTitle:[[NSUserDefaults standardUserDefaults] stringForKey:@"elapsedLabelKey"] forState:UIControlStateNormal]; myButton.highlighted = NO;
以下是我如何设置更新标题的计时器:
- (void)actionTimer { if (myTimer == nil) { myTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector(showActivity) userInfo: nil repeats: YES]; } }
以下是实际更新标题的方法:
- (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]; } }
闪烁不是由定时器本身引起的。 UIButton方法setTitle:forState导致闪烁。 一个简单的解决方法是使用没有标题的UIButton,并叠加一个UILabel。 当您更改UILabel文本时,它不会闪烁。
将buttontypes设置为UIButtonTypeCustom ,它将停止闪烁
* 请注意*
当_button的 “ buttonType ”是“UIButtonTypeSystem”时 ,下面的代码无效 :
[UIView setAnimationsEnabled:NO]; [_button setTitle:@"title" forState:UIControlStateNormal]; [UIView setAnimationsEnabled:YES];
当_button的 “ buttonType ”是“UIButtonTypeCustom”时 ,上面的代码是有效的 。
查看如何在标题更改中停止不需要的UIButtonanimation的回复? :
[UIView setAnimationsEnabled:NO]; [elapsed setTitle:[NSString stringWithFormat:@"%.2i:%.2i:%.2i:%.2i",days,hours,minutes,seconds] forState:UIControlStateNormal]; [UIView setAnimationsEnabled:YES];
比[UIView setAnimationsEnabled:NO]
可能影响其他animation更好的方法是只禁用特定的标题animation。
Objective-C的:
[UIView performWithoutAnimation:^{ [myButton setTitle:text forState:UIControlStateNormal]; [myButton layoutIfNeeded]; }];
Swift 3:
UIView.performWithoutAnimation { myButton.setTitle(text, for: .normal) myButton.layoutIfNeeded() }
默认的“setTitle”行为绝对是可恶的!
我的解决scheme是:
[UIView setAnimationsEnabled:NO]; [_button layoutIfNeeded]; [_button setTitle:[NSString stringWithFormat:@"what" forState:UIControlStateNormal]; [UIView setAnimationsEnabled:YES];
此外,在故事板,button的属性下,我取消选中:
- 反转亮点
- 突出显示的调整图像
并检查:
- 禁用调整图像
testing并在iOS 8上工作。
更新 – Swift
我build议你创build自定义button并覆盖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) } }
您可以简单地为UIButton创build另一个函数,以便将来使用。
extension UIButton { func setTitleWithoutAnimation(_ title: String?, for controlState: UIControlState) { UIView.performWithoutAnimation { self.setTitle(title, for: controlState) self.layoutIfNeeded() } } }
这就对了!
只要像以前那样设置标题,但用setTitle
replacesetTitleWithoutAnimation