验证码 倒计时

来源:互联网 发布:淘宝搜索排名优化规则 编辑:程序博客网 时间:2024/06/14 22:19

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
原创粉丝点击