问题描述
当我在UIButton上调用setTitle时,该按钮在iOS 7中闪烁。我尝试设置myButton.highlighted = NO,但这并没有阻止按钮闪烁。
When I call setTitle on a UIButton, the button flashes in iOS 7. I tried setting myButton.highlighted = NO, but that didn't stop the button from flashing.
[myButton setTitle:[[NSUserDefaults standardUserDefaults] stringForKey:@"elapsedLabelKey"] forState:UIControlStateNormal];
myButton.highlighted = NO;
以下是我如何设置更新标题的计时器:
Here is how I set up the timer that updated the titles:
- (void)actionTimer {
if (myTimer == nil) {
myTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector: @selector(showActivity)
userInfo: nil
repeats: YES];
}
}
以下是实际更新标题的方法:
Here is the method that actually updates the titles:
- (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文本时,它不会闪烁。
The flashing is not caused by the timer per se. The UIButton method setTitle:forState causes the flashing. One simple work around is to use a UIButton with no title and to superimpose a UILabel. When you change the UILabel text, it will not flash.
这篇关于更新标题时如何防止UIButton闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!