美文网首页
60秒倒计时

60秒倒计时

作者: Jessica124 | 来源:发表于2016-11-17 15:05 被阅读25次

NSTimer方式

@interface TimerButton ()

@property (nonatomic, strong, readwrite) NSTimer *timer;

@property (assign, nonatomic) NSTimeInterval durationToValidity;

@property (strong, nonatomic) UIView *lineView;

@end

@implementation TimerButton

- (instancetype)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

self.titleLabel.font = [UIFont systemFontOfSize:14];

self.enabled = YES;

_lineView = [[UIView alloc] initWithFrame:CGRectMake(-10, 5, 0.5, CGRectGetHeight(frame) - 2*5)];

_lineView.backgroundColor = [UIColor redColor];

[self addSubview:_lineView];

}

return self;

}

- (void)setEnabled:(BOOL)enabled{

[super setEnabled:enabled];

UIColor *foreColor = enabled ? [UIColor redColor] : [UIColor greenColor];

[self setTitleColor:foreColor forState:UIControlStateNormal];

if (enabled) {

[self setTitle:@"发送验证码" forState:UIControlStateNormal];

}else if ([self.titleLabel.text isEqualToString:@"发送验证码"]){

[self setTitle:@"正在发送..." forState:UIControlStateNormal];

}

}

- (void)startUpTimer{

_durationToValidity = 60;

if (self.isEnabled) {

self.enabled = NO;

}

[self setTitle:[NSString stringWithFormat:@"%.0f 秒", _durationToValidity] forState:UIControlStateNormal];

self.timer = [NSTimer scheduledTimerWithTimeInterval:1

target:self

selector:@selector(redrawTimer:)

userInfo:nil

repeats:YES];

}

- (void)invalidateTimer{

if (!self.isEnabled) {

self.enabled = YES;

}

[self.timer invalidate];

self.timer = nil;

}

- (void)redrawTimer:(NSTimer *)timer {

_durationToValidity--;

if (_durationToValidity > 0) {

self.titleLabel.text = [NSString stringWithFormat:@"%.0f 秒", _durationToValidity];//防止 button_title 闪烁

[self setTitle:[NSString stringWithFormat:@"%.0f 秒", _durationToValidity] forState:UIControlStateNormal];

}else{

[self invalidateTimer];

}

}

GCD方式

@implementation TimerGCD

- (instancetype)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

self.titleLabel.font = [UIFont systemFontOfSize:14];

self.enabled = YES;

}

return self;

}

- (void)startTime {

NSInteger time = 59;

__block NSInteger timeout = time; //倒计时时间

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_source_t _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, ^{

if(timeout <= 0) { //倒计时结束,关闭

dispatch_source_cancel(_timer);

dispatch_async(dispatch_get_main_queue(), ^{

self.enabled = YES;

});

} else {

NSInteger seconds = timeout % 60;

NSString *strTime = [NSString stringWithFormat:@"%.2ld", (long)seconds];

dispatch_async(dispatch_get_main_queue(), ^{

[self setTitle:[NSString stringWithFormat:@"重发(%@)",strTime] forState:UIControlStateNormal];

// iOS 7

[self setTitle:[NSString stringWithFormat:@"重发(%@)",strTime] forState:UIControlStateDisabled];

self.enabled = NO;

});

timeout--;

}

});

dispatch_resume(_timer);

}

- (void)setEnabled:(BOOL)enabled{

[super setEnabled:enabled];

UIColor *foreColor = enabled ? [UIColor redColor] : [UIColor greenColor];

[self setTitleColor:foreColor forState:UIControlStateNormal];

if (enabled) {

[self setTitle:@"发送验证码" forState:UIControlStateNormal];

}else if ([self.titleLabel.text isEqualToString:@"发送验证码"]){

[self setTitle:@"正在发送..." forState:UIControlStateNormal];

}

}

相关文章

  • 倒计时

    新年倒计时…中考倒计时…高考倒计时…告别学生倒计时…假期倒计时…睡觉倒计时…起床倒计时… 生活中,很多与我们息息相...

  • 倒计时不仅使我们焦躁,还可以使我们振奋啊

    中考倒计时,高考倒计时, 倒计时100天 倒计时50天 倒计时10天 倒计时5天 ........ 这个场景想必在...

  • 倒计时

    文/木子 开学倒计时了 爸妈回去倒计时了 教师节倒计时了 国庆节倒计时了 小儿12岁倒计时了 寒假倒计时了 元旦倒...

  • wordlist 13

    倒计时倒计时

  • wordlist 11

    倒计时倒计时

  • van-count-down 倒计时相关问题

    1. 倒计时重置问题 背景:业务需要在开启倒计时、暂停、恢复倒计时实时判断是否展示倒计时,并重置倒计时 问题:直接...

  • iOS开发-倒计时

    倒计时 倒计时60s 倒计时HH-MM-SS 1.倒计时60s 很多时候在点击按钮发送短信的时候需要倒计时读秒 2...

  • 10.27

    倒计时第一天,倒计时7,好想赶快到倒计时1。

  • 1小时倒计时

    倒计时 目前我们内部用的比较多的倒计时:一个小时的倒计时、12个小时的倒计时。 examinePassTime:审...

  • js中date倒计时

    获取倒计时之前,我们不妨先来看怎样获取当前时间吧!!! 获取倒计时 1、实现倒计时功能2、倒计时结束按钮可被点击,...

网友评论

      本文标题:60秒倒计时

      本文链接:https://www.haomeiwen.com/subject/sebdpttx.html