当我在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,它将停止闪烁
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)
小智 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)
小智 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)
这就对了!
只需像以前一样设置标题,但替换setTitle
为setTitleWithoutAnimation
默认的"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)
归档时间: |
|
查看次数: |
20082 次 |
最近记录: |