验证码计时 -- UIButton setTitle 闪烁问题解决方案

时间:2021-07-20 00:05:49

首先,有各种版本

方法一:

我运用的一种极其简单的版本:  将UIButton的Type 设成 Custom 就不会有闪烁的问题重现

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #35568a }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px }
span.s1 { }
span.s2 { color: #000000 }
span.s3 { color: #c35900 }
span.s4 { color: #cf8724 }
span.s5 { font: 14.0px "PingFang SC"; color: #cf8724 }

@property (strong, nonatomic) IBOutlet UIButton *getCodeBtn;

@property (nonatomic, strong) NSTimer *timerCode; //验证码定时器

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo }
span.s1 { }
span.s2 { color: #35568a }

__block  int leftTime;

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #587ea8 }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px }
span.s1 { }
span.s2 { color: #35568a }
span.s3 { color: #000000 }
span.s4 { color: #587ea8 }

- (void)dealloc {

if ([_timerCode isValid]) {

[_timerCode invalidate];

_timerCode = nil;

}

}

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #777997 }
span.s1 { }

#pragma mark - 验证码

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #cf8724 }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #cf8724 }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #587ea8 }
span.s1 { font: 14.0px Menlo }
span.s2 { }
span.s3 { color: #35568a }
span.s4 { color: #c35900 }
span.s5 { font: 14.0px Menlo; color: #000000 }
span.s6 { color: #000000 }
span.s7 { color: #587ea8 }
span.s8 { font: 14.0px "PingFang SC" }
span.s9 { color: #777997 }

//重新发送验证码

- (IBAction)resendCodeBtnPressed:(UIButton *)sender {

//判断哪一个网络请求发送验证码

//...

sender.enabled = NO;

leftTime = 60;

[self startOneTimer];

//发送验证码请求

[self.viewModel requestWithSendCode:[kUSERDEFAULT objectForKey:_isResetPassword? kForgetMobile:kLoginMobile] block:^(BOOL flag) {

}];

}

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #cf8724 }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #cf8724 }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #587ea8 }
span.s1 { }
span.s2 { font: 14.0px Menlo }
span.s3 { color: #35568a }
span.s4 { color: #000000 }
span.s5 { color: #587ea8 }
span.s6 { color: #c35900 }
span.s7 { color: #e82300 }
span.s8 { font: 14.0px "PingFang SC"; color: #e82300 }
span.s9 { color: #777997 }
span.s10 { color: #cf8724 }
span.s11 { font: 14.0px "PingFang SC"; color: #cf8724 }

/**

*  发送验证码之后等待若干时间

*/

- (void)startOneTimer {

_getCodeBtn.enabled = NO;

leftTime = 60;

_timerCode = [NSTimer  scheduledTimerWithTimeInterval:1 block:^{

if (leftTime == 0) {

_getCodeBtn.enabled = YES;

[_getCodeBtn setTitle:@"重新发送" forState:UIControlStateNormal];

[_getCodeBtn setTitleColor:fcc639 forState:UIControlStateNormal];

//计时完成,销毁定时器

[_timerCode invalidate];

_timerCode = nil;

leftTime = 60;

return ;

}

leftTime--;

[_getCodeBtn setTitle:[NSString stringWithFormat:@"%i秒后重发", leftTime] forState:UIControlStateNormal];

[_getCodeBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];

} repeats:YES];

}

方法二: 就是在UIButton上罩一个同样大小的UILabel,然后每次刷新UILabel的文字,不刷新按钮,效果不错,不再闪烁了

链接 :http://blog.****.net/kyfxbl/article/details/17619221

方法三:https://www.cnblogs.com/manji/p/4813520.html

补充: 我的方法中运用了 防止timer没有被销毁掉的timerBlocks 请自行查找

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #cf8724 }
span.s1 { }

//  NSTimer+Blocks.h

//

//  Created by Jiva DeVoe on 1/14/11.

//  Copyright 2011 Random Ideas, LLC. All rights reserved.