验证码 倒计时

import “MERootController.h”

import “MelPageView.h”

import “MelView.h”

import “SecondController.h”

@interface MERootController ()

@property (nonatomic,strong)MelView *myView;
@property (nonatomic,strong)UIButton *firstBtn;

@property (nonatomic,strong)UIButton *secondBtn;
@end

@implementation MERootController

  • (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
    self.title = @”水平菜单栏”;
    [self creatView];
    }
    -(void)creatView
    {
    //创建病初始化添加视图
    self.myView = [[MelView alloc]init];
    self.myView.frame = CGRectMake(0, 64, self.view.frame.size.width, 45);
    self.myView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.myView];

    //self.title = @”菜单分段”;
    //设置菜单名数组
    NSArray * menuArray = [NSArray arrayWithObjects:@”行程”,@”产品特色”, nil];
    [self.myView setNameWithArray:menuArray];
    //5.设置委托代理
    self.myView .myDelegate = self;

    UIButton *button= [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(200, 180,100 , 80);
    button.backgroundColor = [UIColor grayColor];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitle:@”点 击” forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnClikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    button.hidden = YES;
    self.firstBtn = button;

    UIButton *button1= [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(100, 180,100, 80);
    button1.backgroundColor = [UIColor grayColor];

    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button1 setTitle:@”获取验证码” forState:UIControlStateNormal];
    button1.titleLabel.font= [UIFont systemFontOfSize:15];
    button1.backgroundColor =[UIColor orangeColor];
    [button1 addTarget:self action:@selector(secondClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    self.secondBtn = button1;

}
-(void)secondClick:(UIButton *)buton
{

[self startTime];

}

pragma mark- 倒计时

-(void)startTime
{

__block int timeOut = 30;//倒计时时间;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);

dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0);//每秒执行

dispatch_source_set_event_handler(_timer, ^{

   if (timeOut<=0) {    //倒计时结束

       dispatch_source_cancel(_timer);
       dispatch_async(dispatch_get_main_queue(), ^{

           self.secondBtn.backgroundColor =[UIColor clearColor];
           [self.secondBtn setTitle:@"重新发送" forState:UIControlStateNormal];
           [self.secondBtn setTitleColor:[UIColor colorWithRed:255/255.0 green:153/255.0 blue:102/255.0 alpha:1] forState:UIControlStateNormal];
           self.secondBtn.backgroundColor =[UIColor grayColor];
           self.secondBtn.titleLabel.font =[UIFont systemFontOfSize:15];
           self.secondBtn.userInteractionEnabled = YES;
       });
   }else
   {
       int seconds  = timeOut %61;
       NSString *timerStr =[NSString stringWithFormat:@"%.2d",seconds];
       //开启异步队列
       dispatch_async(dispatch_get_main_queue(), ^{

           [UIView beginAnimations:nil context:nil];
           [UIView setAnimationDuration:1];
           [self.secondBtn setTitle:[NSString stringWithFormat:@"%@秒后重新发送",timerStr] forState:UIControlStateNormal];

           [self.secondBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
           self.secondBtn.titleLabel.textColor = [UIColor whiteColor];
           self.secondBtn.titleLabel.font =[UIFont systemFontOfSize:12];

           [UIView commitAnimations];

           self.secondBtn.userInteractionEnabled =NO;
       });

       timeOut--;
   }

});

dispatch_resume(_timer);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值