描述:定时器改变 UIButton 文本显示时出现闪动 !
解决:
方案一:UIButton 的 UIButtonType 类型设置为 UIButtonTypeCustom 。PS:使用Xib或SB创建 UIButton 时默认 UIButtonTypeSystem 类型 。
方案二:若 UIButtonType 已经设置 UIButtonTypeSystem 类型 ,titleLabel.text 与 setTitle 同时设置 。
- (void)viewDidLoad { [super viewDidLoad]; _btn = [UIButton buttonWithType:UIButtonTypeSystem]; [_btn setFrame:CGRectMake(100, 100, 100, 30)]; _btn.backgroundColor = [UIColor redColor]; [self.view addSubview:_btn]; _countDown = 60; [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeCount:) userInfo:nil repeats:YES]; } #pragma mark ------ timeCount - (void)timeCount:(NSTimer *)timer{ if (_countDown > 0) { _countDown --; // 同时设置 _btn.titleLabel.text = [NSString stringWithFormat:@"%ld 计时",(unsigned long)_countDown]; [_btn setTitle:[NSString stringWithFormat:@"%ld 计时",(unsigned long)_countDown] forState:UIControlStateNormal]; } else { [timer invalidate]; timer = nil; } }
时间: 2024-10-07 06:29:18