首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何防止UIButton在更新标题时闪烁

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

Stack Overflow用户
提问于 2013-10-15 08:42:05
回答 6查看 24.8K关注 0票数 90

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

代码语言:javascript
运行
复制
[myButton setTitle:[[NSUserDefaults standardUserDefaults] stringForKey:@"elapsedLabelKey"] forState:UIControlStateNormal];

myButton.highlighted = NO;

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

代码语言:javascript
运行
复制
- (void)actionTimer {
    if (myTimer == nil) {

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

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

代码语言:javascript
运行
复制
- (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];
    }
}
EN

回答 6

Stack Overflow用户

发布于 2016-08-22 12:57:21

与可能影响其他动画的[UIView setAnimationsEnabled:NO]相比,一种更好的方法是仅禁用特定的标题动画。

Objective-C:

代码语言:javascript
运行
复制
[UIView performWithoutAnimation:^{
  [myButton setTitle:text forState:UIControlStateNormal];
  [myButton layoutIfNeeded];
}];

Swift:

代码语言:javascript
运行
复制
UIView.performWithoutAnimation { 
    myButton.setTitle(text, for: .normal)
    myButton.layoutIfNeeded()
}
票数 62
EN

Stack Overflow用户

发布于 2013-11-01 12:54:47

查看来自How to stop unwanted UIButton animation on title change?的响应:

代码语言:javascript
运行
复制
[UIView setAnimationsEnabled:NO];
[elapsed setTitle:[NSString stringWithFormat:@"%.2i:%.2i:%.2i:%.2i",days,hours,minutes,seconds] forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
票数 19
EN

Stack Overflow用户

发布于 2017-06-20 07:36:30

您可以简单地为UIButton创建另一个函数,以便将来使用。

代码语言:javascript
运行
复制
extension UIButton {
    func setTitleWithoutAnimation(_ title: String?, for controlState: UIControlState) {
        UIView.performWithoutAnimation {
            self.setTitle(title, for: controlState)
            self.layoutIfNeeded()
        }
    }
}

就是这样!

只需像以前一样设置标题,但请用setTitleWithoutAnimation替换setTitle

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19371522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档