首页 > 技术文章 > 短信验证倒计时按钮

modaoyicheng 2015-09-22 10:05 原文

#import <UIKit/UIKit.h>


@interface SecondsButton : UIButton


- (void)startCountdown;

//- (void)pauseCountdown;

//- (void)resumeCountdown;


@end

 --------------------------

#import "SecondsButton.h"


#define SECOND 60


@interface SecondsButton ()

{

    NSTimer *_timer;

    NSInteger _seconds;

}

@end


@implementation SecondsButton


- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        self.opaque = YES;

        

        self.titleLabel.font = [UIFont systemFontOfSize:12.0f];

    

//        [self setBackgroundImage:[zhuiUtils imageWithColor:[UIColor colorWithHexString:@"a7a9ab"] andSize:CGSizeMake(frame.size.width, frame.size.height)] forState:UIControlStateDisabled];

//        [self setBackgroundImage:[zhuiUtils imageWithColor:[UIColor colorWithHexString:@"5aa4d1"] andSize:CGSizeMake(frame.size.width, frame.size.height)] forState:UIControlStateNormal];

//        

//        [self setBackgroundImage:[zhuiUtils imageWithColor:[UIColor colorWithHexString:@"3781ae"] andSize:CGSizeMake(frame.size.width, frame.size.height)] forState:UIControlStateHighlighted];

        

        _seconds = SECOND;

        

        NSString *title = lnreget(_seconds);

        

        [self setTitle:title forState:UIControlStateDisabled];

        [self setTitle:lngetcheckcode forState:UIControlStateNormal];


    }

    return self;

}


#pragma mark - public methods

- (void)startCountdown

{

    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];

    

    self.enabled = NO;

}

//- (void)pauseCountdown

//{

//    if (![_timer isValid]) {

//        return ;

//    }

//    

//    //[self setFireDate:[NSDate dateWithTimeIntervalSinceNow:0]];

//    [_timer setFireDate:[NSDate distantFuture]]; //如果给我一个期限,我希望是4001-01-01 00:00:00 +0000

//}

//

//- (void)resumeCountdown

//{

//    if (![_timer isValid]) {

//        return ;

//    }

//    

//    //[self setFireDate:[NSDate dateWithTimeIntervalSinceNow:0]];

//    [_timer setFireDate:[NSDate date]];

//

//}


//-(void)release

//{

//    if (timer) {

//        if ([timer respondsToSelector:@selector(isValid)]) {

//            if ([timer isValid]) {

//                [timer invalidate];

//                seconds = 60;

//            }

//        }

//    }

//}


#pragma mark - NSTimer private methods

- (void)timerFireMethod:(NSTimer*)theTimer

{

    if (_seconds == 0) {

        if ([_timer respondsToSelector:@selector(isValid)]) {

            if ([_timer isValid]) {

                [_timer invalidate];

                _timer = nil;

            }

        }

        _seconds = SECOND;

        [self setTitle:lngetcheckcode forState:UIControlStateNormal];

        self.enabled = YES;

    }else{

        _seconds--;

        NSString *title = lnreget(_seconds);

        

        [self setTitle:title forState:UIControlStateDisabled];

        

    }

}


#pragma mark - Memory methods

-(void)dealloc

{

    if (_timer) {

        if ([_timer respondsToSelector:@selector(isValid)]) {

            if ([_timer isValid]) {

                [_timer invalidate];

                

            }

        }

    }

//    [super dealloc];

}


@end

 

推荐阅读