2012-08-30 141 views
-1
一個UIImageView

我具有由圖像的陣列,在其上我然後運行的動畫延遲上在IOS

[myUIImageView startAnimating] 

我希望動畫一旦運行,則停止對3秒,然後重複。

我需要運行這個動畫在一個單獨的線程,這樣我在viewDidLoad中有

NSThread *animationThread = [[NSThread alloc] initWithTarget:self selector:@selector(startAnimTask) withObject:nil waitUntilDone:NO]; 
[animationThread start]; 

,然後

-(void) startAnimTask { 

    //create array of images here; animationDuration (3) and animationRepeatCount (1) 
    [self setUpAnimation]; 

    while (true){ 
     [myUIImageView startAnimating]; 
     usleep(3);   
     [myUIImageView stopAnimating]; 
     usleep(3);  
    } 
} 

使用這種方法,我收到一個內存警告。我也試過在MainThread上運行啓動和停止,但沒有運氣。

任何想法?

回答

5

這樣做:

[myUIImageView startAnimating]; 
[self performSelector:@selector(startAnimTask) 
     withObject:nil 
     afterDelay:(myUIImageView.animationDuration+3.0)]; 

現在選擇的是:

-(void)startAnimTask 
{ 
    [myUIImageView startAnimating]; 
    //repeat again then add above selector code line here 
} 
+1

如果不是這種是performSelectorOnMainThread?它似乎不工作,只是執行選擇器 –

+0

什麼//重複然後參考? –

1

UI不是線程安全的,所以UI調用應該只在主線程中執行,也許是這種情況。

1

我想你可以用這個去:

[self performSelector:@selector(startAnimTask) 
      withObject:nil 
      afterDelay:0.1]; 

    [self performSelector:@selector(startAnimTask) 
      withObject:nil 
      afterDelay:3.0]; 

startAnimTask方法編寫動畫邏輯代碼。

享受編碼:)

1

試試這個辦法來完成你的任務:

[self performSelector:@selector(myTask) 
      withObject:nil 
      afterDelay:3.0]; 

-(void)myTask{ 

//Write your logic for animation 

} 
+0

這就是我說的你有什麼不同 –