美文网首页
OC-UIButton-发送验证码倒计时

OC-UIButton-发送验证码倒计时

作者: trinity_ | 来源:发表于2016-09-30 09:31 被阅读33次

    创建文件,继承UIButton

    .h中

    #import@interface LButton : UIButton

    - (void)startWithTimerCount:(NSInteger)count;

    - (void)stopTimer;

    @end

    .m中

    @interface LButton ()

    {

    NSInteger timerCount;

    }

    @property (nonatomic, strong) NSTimer *timerCoder;

    @property (nonatomic) BOOL buttonEnabled;

    @end

    @implementation LButton

    - (void)startWithTimerCount:(NSInteger)count

    {

    [self stopTimer];

    NSString *buttonTitle = [[NSString strWithIntNum:count] stringByAppendingString:@"秒后重发"];

    [self setButtonTitle:buttonTitle];

    self.buttonEnabled = NO;

    timerCount = count-1;

    _timerCoder = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];

    }

    - (void)stopTimer

    {

    if (_timerCoder) {

    [_timerCoder invalidate];

    _timerCoder = nil;

    self.buttonEnabled = YES;

    }

    }

    - (void)timerAction {

    if (timerCount > 0) {

    NSString *title = [[NSString strWithIntNum:timerCount] stringByAppendingString:@"秒后重发"];

    [self setButtonTitle:title];

    }else

    {

    [self stopTimer];

    }

    timerCount -= 1;

    }

    - (void)setButtonTitle:(NSString *)title {

    self.titleLabel.text = title;

    [self setTitle:title forState:UIControlStateNormal];

    }

    - (void)setButtonEnabled:(BOOL)buttonEnabled

    {

    _buttonEnabled = buttonEnabled;

    if (buttonEnabled) {

    [self setButtonTitle:@"重新发送"];

    self.alpha = 1.0;

    }else

    {

    self.alpha = 0.5;

    }

    self.enabled = buttonEnabled;

    }

    @end

    相关文章

      网友评论

          本文标题:OC-UIButton-发送验证码倒计时

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