【问题标题】:Loading in new Thread加载新线程
【发布时间】:2013-03-08 12:51:10
【问题描述】:

我正在尝试在调用 Web 服务时进行加载视图... 我试过这个:

-(void) viewDidLoad {
[NSThread detachNewThreadSelector:@selector(workerThread) toTarget:self withObject:nil];
//here I call the thread before call web service
 }


 -(void) workerThread {
[viewLoading setHidden:NO];
[NSThread detachNewThreadSelector:@selector(threadAnimacao) toTarget:self withObject:nil];
}


 -(void) threadAnimacao { //call this function is ok, but the timerLoadingEvento only is called after the call webservice ends
contadorLoading = 0;
nVoltas = 0;
nEspera = 0;
timerLoading = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerLoadingEvento:) userInfo:nil repeats:YES]; 
}


-(void)timerLoadingEvento:(NSTimer *)theTimer{
UIImageView *img = imgLoading;

if (nEspera == 0) {
    if(contadorLoading == 0)
        [img setImage:[UIImage imageNamed:@"frame_01.png"]];
    else if(contadorLoading == 1)
        [img setImage:[UIImage imageNamed:@"frame_02.png"]];
    else if(contadorLoading == 2)
        [img setImage:[UIImage imageNamed:@"frame_03.png"]];
    else if(contadorLoading == 3)
        [img setImage:[UIImage imageNamed:@"frame_04.png"]];
    else if(contadorLoading == 4)
        [img setImage:[UIImage imageNamed:@"frame_05.png"]];
    else if(contadorLoading == 5)
        [img setImage:[UIImage imageNamed:@"frame_06.png"]];
    else if(contadorLoading == 6)
        [img setImage:[UIImage imageNamed:@"frame_07.png"]];
    else if(contadorLoading == 7)
        [img setImage:[UIImage imageNamed:@"frame_08.png"]];
    else if(contadorLoading == 8)
        [img setImage:[UIImage imageNamed:@"frame_09.png"]];
}

contadorLoading++;
nVoltas++;

if(nEspera != 0)
    nEspera++;

if(nEspera>250) {
    nEspera = 0;
    contadorLoading = 0;
    nVoltas = 0;
}

if(contadorLoading == 8)
    contadorLoading = 0;

if(nVoltas == 9)
    nEspera = 1;
 }

【问题讨论】:

  • 不要在线程中更新 UI。

标签: objective-c xcode ios4


【解决方案1】:

您可以在 Web 服务调用结束时使用默认活动指示器

或者,如果您正在为您的进度定制图像

您可以尝试将动画图像(UIImage 对象数组)添加到 UImageView,设置动画重复计数(0 表示无限期)和图像视图的动画持续时间并调用 imageview 开始动画以及一旦网络服务完成调用停止动画。

查看 UIImageView API 指南了解更多详情。

希望对你有帮助!!!

【讨论】:

  • 当您在 UIImageView 中内置 API 以循环播放图像时,为什么要使用自己的计时器。
  • 尝试在主线程中调用workerThread函数。就像在 ViewDidLoad 调用 [self workerThread]
  • 非常感谢!它有效...图像名称错误..animationImages 效果很好!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-21
  • 1970-01-01
  • 2012-05-17
相关资源
最近更新 更多