objective-c - ios开发验证码倒计时60s,真机测试退到后台,再次打开发现秒数没有变化,怎么办?
PHPz
PHPz 2017-04-17 17:51:26
0
7
372

__block int timeout=60;

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.validBtn setTitle:@"获取短信验证码" forState:UIControlStateNormal];
            //self.validBtn.backgroundColor=RGB(120, 0, 103);
            [self.validBtn setBackgroundImage:[UIImage imageNamed:@"获取验证码"] forState:UIControlStateNormal];
            self.validBtn.userInteractionEnabled = YES;
        });
    }else{
        int seconds = timeout;
        NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.validBtn setTitle:[NSString stringWithFormat:@"%@秒",strTime] forState:UIControlStateNormal];
            //self.validBtn.backgroundColor=RGB(207, 207, 207);
            [self.validBtn setBackgroundImage:[UIImage imageNamed:@"获取验证码-不可点击"] forState:UIControlStateNormal];
            self.validBtn.userInteractionEnabled = NO;
        });
        timeout--;
    }
});
dispatch_resume(_timer);
PHPz
PHPz

学习是最好的投资!

reply all(7)
Peter_Zhu

Terminate the thread when leaving the current page, and then initialize the countdown to 60

伊谢尔伦

The timer needs to run in the Runloop

刘奇

After you go to the background, if you are not declared as a program that can run in the background, NSTimer should not go away.
If it were me:
1. Stop the Timer before entering the background, record the time in the applicationDidEnterBackground of the appdelegate, compare the time when entering the foreground, calculate the difference, refresh the verification code value display, and then start the Timer.
2. Stop the Timer before entering the background, or use backgroundTask to decrement the verification code number every few seconds in the background, return to the front desk, and then start the Timer. (This is so boring...)
3. Declare your App as a program that can run in the background.
I would choose the first one. . .

洪涛

My plan is as follows:

  1. Record start time.

  2. Update the display on the interface every second, use Current Time - Start Time to get the number of seconds displayed on the interface当前时间 - 开始时间 得到秒数显示在界面上

  3. 我使用的是 GCD Timer

I am using GCD Timer. There is no need to additionally handle the situation when entering the background and then returning to the foreground. Of course, it is no problem to use other ones. Just handle the things here. 🎜🎜 🎜
PHPzhong

You can try the following solutions.

  __block long totalComplete = 0;
  dispatch_source_set_event_handler(source, ^{
    long value = dispatch_source_get_data(source);
    totalComplete += value;
    self.progressView.progress = (CGFloat)totalComplete/100.0f;
  });
大家讲道理

GreedTimer can solve the problem you mentioned. When using GRTimertimeInBackground设置为YES

左手右手慢动作

None of the methods mentioned above work. I found a simple and feasible one.

http://www.jianshu.com/p/8e61...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!