添加倒计时后出现的bug

来源:互联网 发布:营销数据分析书籍 编辑:程序博客网 时间:2024/06/06 00:47

添加倒计时后出现的bug

添加倒计时后如果在计时结束前点跳过会出现两次进入RootViewController,原因是没结束时间,添加dispatch_source_cancel(_timer);即可

[btn addTarget:selfaction:@selector(goToMain)forControlEvents:UIControlEventTouchUpInside];


-(void)countDown

{

        __block NSInteger timeOut = 5;

        dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

        _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_async(dispatch_get_main_queue(), ^{

                    [selfgoToMain];

                });

            } else {

                int allTime = (int)5 +1;

                int seconds = timeOut % allTime;

                NSString *timeStr = [NSStringstringWithFormat:@"%0.2d", seconds];

                dispatch_async(dispatch_get_main_queue(), ^{

                    UILabel *lab=[self.windowviewWithTag:6666];

                    lab.text=[NSStringstringWithFormat:@"%@s",timeStr];

                });

                timeOut--;

            }

        });

        dispatch_resume(_timer);

}

- (void)goToMain

{

    dispatch_source_cancel(_timer);

    

//RootViewController

}


1 0
原创粉丝点击