zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

iOS开发 - 数字快速变化到指定数字

ios开发 快速 数字 指定 变化
2023-09-11 14:21:22 时间

这里写图片描述

这样的功能在很多地方都有见过,所以就用线程队列里的数据源来实现了下,看下代码:

- (void)countJumpAction
{



    __block int _numText = 0;
    //全局队列    默认优先级
    dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //定时器模式  事件源
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, quene);
    //NSEC_PER_SEC是秒,*1是每秒
    dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), NSEC_PER_SEC * 0.00003, 0);
    //设置响应dispatch源事件的block,在dispatch源指定的队列上运行
    dispatch_source_set_event_handler(timer, ^{
        //回调主线程,在主线程中操作UI
        dispatch_async(dispatch_get_main_queue(), ^{
            if (_numText <= 9999) {
                _countJump.text = [NSString stringWithFormat:@"%.5d",_numText];
                _numText++;

            }
            else
            {
                //这句话必须写否则会出问题
                dispatch_source_cancel(timer);
                _countJump.text = @"98461";
            }
        });
    });
    //启动源
    dispatch_resume(timer);

}

这里的知识点和上一篇数字跳变器是一样的,不一样的是操作了数字的递变,在指定时间内让数字递加并通过UI快速展示,给人造成一种数字快速变化的视觉效果。

下载地址