First, there are various versions
Method One:
An extremely simple version of my application: Setting the type of UIButton to Custom will not reproduce the problem of flicker
@property (strong, nonatomic) iboutlet UIButton *getcodebtn;
@property (nonatomic, strong) Nstimer *timercode; // Verification Code timer
__block int lefttime;
-(void) dealloc {
if ([_timercode isValid]) {
[_timercode invalidate];
_timercode = nil;
}
}
#pragma mark-Verification code
Resend Verification Code
-(ibaction) resendcodebtnpressed: (UIButton *) Sender {
// determine which network request to send a verification code
//...
Sender. enabled = NO;
lefttime = ;
[self startonetimer];
// Send verification code request
[self. ViewModel requestwithsendcode: [kuserdefault objectforkey:_isresetpassword ? kforgetmobile:kloginmobile] block: ^ (BOOL flag) {
}];
}
/**
* wait some time after sending verification code
*/
-(void) Startonetimer {
_getcodebtn. Enabled = NO;
lefttime = ;
_timercode = [nstimer scheduledtimerwithtimeinterval:1 block: ^{
if (lefttime = = 0) {
_getcodebtn. Enabled = YES;
[_getcodebtn settitle:" resend " forstate: UIControlStateNormal];
[_getcodebtn settitlecolor:fcc639 forstate:uicontrolstatenormal];
// timer complete, destroy timer
[_timercode invalidate];
_timercode = nil;
lefttime = ;
return ;
}
lefttime--;
[_getcodebtn settitle:[nsstring Stringwithformat:@ "%i seconds after re-sending lefttime] forstate:uicontrolstatenormal
[_getcodebtn settitlecolor: [uicolor lightgraycolor] forstate: UIControlStateNormal];
} repeats:YES];
}
Method Two: Is on the UIButton cover a same size uilabel, and then each time the Uilabel text refresh, do not refresh the button, the effect is good, no longer flashing
Links: http://blog.csdn.net/kyfxbl/article/details/17619221
Method Three: Https://www.cnblogs.com/manji/p/4813520.html
Add: My method uses to prevent the timer not to be destroyed the timerblocks please search by oneself
Nstimer+blocks.h
//
Created by Jiva DeVoe on 1/14/11.
Copyright: Random Ideas, LLC. All rights reserved.
Verification code Timing--UIButton settitle flicker Problem Solution