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

2016-09-30  本文已影响33人  trinity_

创建文件,继承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

上一篇下一篇

猜你喜欢

热点阅读