ios开发,发验证码倒计时按钮的实现

来源:互联网 发布:淘宝店铺花呗分期 编辑:程序博客网 时间:2024/06/06 03:49



#import "ViewController.h"


#define SEC 60


@interface ViewController ()

{

   UIButton *_verifyBtn;

   UILabel  *_timeLab;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    

    //获取验证码按钮

    _verifyBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

   _verifyBtn.frame =CGRectMake(0,0, 100, 45);

    _verifyBtn.center =CGPointMake([UIScreenmainScreen].bounds.size.width/2.0, [UIScreen mainScreen].bounds.size.height/2.0);

    [_verifyBtnsetBackgroundImage:[UIImageimageNamed:@"yanzhengma_02"]forState:UIControlStateNormal];

    [_verifyBtnsetTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];

    [_verifyBtnaddTarget:selfaction:@selector(verifyEvent)forControlEvents:UIControlEventTouchUpInside];

    _verifyBtn.titleLabel.font = [UIFontsystemFontOfSize:17];

    [self.viewaddSubview:_verifyBtn];

    

    

    _timeLab = [[UILabelalloc] initWithFrame:CGRectMake(0,0, 100,45)];

    _timeLab.backgroundColor = [UIColorclearColor];

    _timeLab.textAlignment =NSTextAlignmentCenter;

    _timeLab.font = [UIFontsystemFontOfSize:16];

    _timeLab.textColor = [UIColorwhiteColor];

   _timeLab.text =@"获取验证码";

    [_verifyBtn addSubview:_timeLab];

    

}


- (void)verifyEvent

{

    //启动倒计时

    [selfperformSelector:@selector(reflashGetKeyBt:)withObject:[NSNumbernumberWithInt:SEC]afterDelay:0];

}


//倒数

- (void)reflashGetKeyBt:(NSNumber *)second

{

   if ([second integerValue] ==0)

    {

        [_verifyBtnsetBackgroundImage:[UIImageimageNamed:@"yanzhengma_02"]forState:UIControlStateNormal];

        

       _timeLab.text =@"获取验证码";

    }

   else

    {

        [_verifyBtnsetBackgroundImage:[UIImageimageNamed:@"yanzhengma_01"]forState:UIControlStateNormal];

        

       int i = [second intValue];

       _timeLab.text=[NSStringstringWithFormat:@"%i秒后重发",i];

        [selfperformSelector:@selector(reflashGetKeyBt:)withObject:[NSNumbernumberWithInt:i-1]afterDelay:1];

    }


}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



0 0