今天發(fā)現(xiàn)動(dòng)態(tài)改變button title時(shí)會(huì)出現(xiàn)閃動(dòng).很蛋疼的問題,后來經(jīng)過反復(fù)嘗試,發(fā)現(xiàn)一個(gè)解決方案.
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC,? 0);
dispatch_source_set_event_handler(timer, ^{
NSInteger scondes = timeout % 60;
dispatch_async(dispatch_get_main_queue(), ^{
[_timerBtn setBackgroundColor:[UIColor blueColor]];
_timerBtn.titleLabel.text = [NSString stringWithFormat:@"%ld秒",scondes];
[_timerBtn setTitle:[NSString stringWithFormat:@"%ld秒",scondes] forState:UIControlStateNormal];
});
timeout --;
});
dispatch_resume(timer);
注意這個(gè)方法里
_timerBtn.titleLabel.text = [NSString stringWithFormat:@"%ld秒",scondes];
[_timerBtn setTitle:[NSString stringWithFormat:@"%ld秒",scondes] forState:UIControlStateNormal];
如果單獨(dú)設(shè)置_timerBtn.titleLabel.text = [NSString stringWithFormat:@"%ld秒",scondes];賦值,label是不顯示文字的.如果只設(shè)置[_timerBtn setTitle:[NSString stringWithFormat:@"%ld秒",scondes] forState:UIControlStateNormal];這個(gè)的話,就會(huì)出現(xiàn)閃動(dòng)的效果..后來經(jīng)過嘗試,發(fā)現(xiàn)必須兩個(gè)都存在,而且_timerBtn.titleLabel.text = [NSString stringWithFormat:@"%ld秒",scondes];這個(gè)要放在[_timerBtn setTitle:[NSString stringWithFormat:@"%ld秒",scondes] forState:UIControlStateNormal];前邊,至于為什么這么做我也沒找到合適的解釋...
還有個(gè)方法就是用xib的時(shí)候button不用system改為custom,同理十电,代碼用custom也不會(huì)閃