2012-08-30 68 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

这就是我说的你有什么不同 –