iPhone:检测UIImageView图像序列animation结束的最佳方法

我们知道UIImageView对图像序列animation有很好的支持。 我们可以很容易地创build一个UIImage对象数组,设置animationImages属性,configurationanimation持续时间,重复计数等,然后就可以了。 但似乎没有办法知道这个animation何时结束。

说我有十个图像,然后我想与他们运行一个animation(重复计数= 1)。 而当animation结束时,我想运行一些其他的代码。 知道animation已经结束的最好方法是什么?

我已经明白,我可以创build一个NSTimer并安排在animation持续时间后触发。 但是如果你需要高精度的话,你真的不能依赖计时器。

所以我的问题是,有没有更好的方式来知道UIImageView图像序列animation已经结束,而不使用计时器?

代码是这样的

 myImageView.animationImages =图片;  //图像是UIImages的NSArray
 myImageView.animationDuration = 2.0;
 myImageView.animationRepeatCount = 1;

 [myImageView startAnimating]

UIImageView上的isAnimating属性在animation完成时应该为NO 。 这不是一个正式的财产,所以你不能build立观察。 你可以在细粒度的定时器(如CADisplayLink 's)上进行轮询。

如果这是你正在寻找的东西,那么没有“animation完成”的代表。 时间可以根据图像的加载延迟等因素而变化,不,没有确切的方法来确切地知道何时完成。

在UIImageView上的图像animation是一个方便,而不是重量级足以做严肃的animation工作。 如果你需要这种精确度,考虑滚动你自己。

我做了这个(我的UIImageView子类的方法)。

 -(void)startAnimatingWithCallback:(UIImageViewAnimationCompletitionBlock) completitionCallback { [self startAnimating]; dispatch_queue_t animatingQueue = dispatch_get_current_queue(); dispatch_queue_t pollingQueue = dispatch_queue_create("pollingQueue", NULL); dispatch_async(pollingQueue, ^{ while(self.isAnimating) { usleep(10000); } dispatch_async(animatingQueue, ^{ if (completitionCallback) completitionCallback(); }); }); } 

简单的用法:

 [self.oneView fadeOut]; [self.otherView startAnimatingWithCallback:^ { [self.oneView fadeIn]; }]; 

此外,我可以推荐在animation的第一帧设置UIImageView(属性image )的默认图像,并在启动animation( startAnimating方法)之后将其更改为最后一帧。 这样我们就避免了animation结束后可能发生的丑陋的滚动,但是不会调用callback。