2013-03-08 16 views
0

我想,而我調用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; 
} 
+0

不要更新線程中的UI。 – 2013-03-08 12:57:26

回答

1

您可以使用默認的活動指標,而你的Web服務調用是fiinshes

或者,如果你有你的進步

自定義圖像您可以嘗試添加animationImages(的UIImage對象的數組)UImageView,設置動畫重複次數(0無限期)&動畫的持續時間爲imageview &呼叫imageview開始動畫&一旦Web服務完成呼叫停止動畫。

有關更多詳細信息,請查看UIImageView API指南。

希望這有助於!

+0

我有多個圖像,我已經改變了imageView圖像與定時器.. – Ladessa 2013-03-08 12:58:56

+0

爲什麼你要使用自己的計時器,當你已經在UIImageView API中建立動畫圖像循環。 – 2013-03-08 13:01:43

+0

嘗試在主線程中調用workerThread函數。就像在ViewDidLoad中調用[self workerThread] – 2013-03-08 13:08:52

相關問題