简体   繁体   English

iOS中UIImageView上的动画之间的延迟

[英]Delay between animations on a UIImageView in iOS

I have an animation which is made up of an array of images, on which I then run 我有一个由一系列图像组成的动画,然后在其上运行

[myUIImageView startAnimating]

I want the animation to run once, then stop for 3 seconds, then repeat. 我希望动画运行一次,然后停止3秒钟,然后重复。

I need to run this animation in a separate thread so I have 我需要在单独的线程中运行此动画,所以我有

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

in my viewDidLoad, and then 在我的viewDidLoad中,然后

-(void) startAnimTask {

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

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

Using this method, I receive a memory warning. 使用此方法,我收到内存警告。 I've also tried running the start and stop on the MainThread with no luck. 我也尝试过在运气好的时候在MainThread上运行启动和停止。

Any ideas? 有任何想法吗?

Do this: 做这个:

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

Now selector is: 现在选择器是:

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

UI不是线程安全的,因此UI调用应仅在主线程中执行,也许是这种情况。

I think you can go with this: 我认为您可以这样做:

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

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

In startAnimTask method write your animation logic code. startAnimTask方法中,编写动画逻辑代码。

Enjoy Coding :) 享受编码:)

Try this approach to complete your task: 尝试使用这种方法来完成您的任务:

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

-(void)myTask{

//Write your logic for animation

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM