zoukankan      html  css  js  c++  java
  • 短信验证倒计时按钮

    #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

  • 相关阅读:
    各种素材类精品网站
    Java如何设置代理ip
    Spring事务的实现方式和原理以及隔离级别?
    Spring 是什么?
    线程池中阻塞队列的作用?为什么是先添加队列而不是先创建最大线程?
    为什么用线程池?解释下线程池参数
    并发三大特性
    ThreadLocal的原理和使用场景。
    谈谈对线程安全的理解
    sleep,wait,join,yield
  • 原文地址:https://www.cnblogs.com/modaoyicheng/p/4828120.html
Copyright © 2011-2022 走看看