代码之家  ›  专栏  ›  技术社区  ›  user3459648

如何减少显示加载程序的初始时间?

  •  0
  • user3459648  · 技术社区  · 11 年前

    我在按钮操作中实现加载器,但当我单击按钮时,需要3到5秒才能开始加载。我使用两个控制器来实现加载器-UIImage+animatedGIF.h来实现gif图像和MBProgressHUD.h来实现进度时间。我不知道如何减少启动加载器所需的初始时间。 我正在使用此代码实现加载器-

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"loadingg" withExtension:@"gif"];
    
        self.loader.image = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];
    
    
        float progress = 0.0f;
    
        while (progress < 1.0f) {
    
    
    
            progress += 0.01f;
    
           // HUD.progress = progress;
    
            usleep(50000);
    
    
        }
    
        [NSTimer scheduledTimerWithTimeInterval:progress target:self selector:@selector(abcd) userInfo:nil repeats:NO];     
    
    2 回复  |  直到 11 年前
        1
  •  0
  •   Community Mohan Dere    8 年前

    我必须假设 usleep(50000) 在主螺纹上。

    如果是,那就是你的 5秒 延迟休眠主线程就是这样:等待。正确的方法是异步加载。

    阅读@bbum建议,了解何时调用usleep: what is the difference among sleep() , usleep() & [NSThread sleepForTimeInterval:]?

        2
  •  0
  •   user3747345    11 年前

    原因是,在您从代理返回之前,它不会显示您的图像,并且由于您使用while循环阻止代理5秒,因此您将体验到您所描述的行为

    推荐文章