iOS 简单的倒计时封装

来源:互联网 发布:java string gbk utf8 编辑:程序博客网 时间:2024/06/08 03:01

- (void)start

{

   _time = 60;

   __block CountdownButton *blockSelf =self;

   if (_timer) {

       _timer = nil;

    }

      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

       self.enabled =NO;

        blockSelf->_timer=[NSTimerscheduledTimerWithTimeInterval:1.0

                                                          target:blockSelf

                                                        selector:@selector(timeAction:)

                                                        userInfo:nil

                                                         repeats:YES] ;

        [[NSRunLoopcurrentRunLoop] addTimer:blockSelf->_timerforMode:NSDefaultRunLoopMode];

        [[NSRunLoopcurrentRunLoop] run];

    });


    

}


- (void)timeAction:(NSTimer *)timer

{

    --_time ;

   NSString *timeStr = [NSStringstringWithFormat:@"%d秒后重发",_time];

    [selfsetTitle:timeStr forState:UIControlStateDisabled];

    

   if (_time ==0) {

        [selfstop];

    }


}


- (void)stop

{

    [_timerinvalidate];

   _timer = nil;

   _time = 60;

   self.enabled =YES;

    [selfsetTitle:@"获取短信验证码" forState:UIControlStateNormal];


}


0 0
原创粉丝点击