天天看點

ios--UIImageView的屬性之animationImages詳解

原文轉自:http://blog.sina.com.cn/s/blog_bf9843bf0101fmwd.html

animationImages是數組類型,該數組必須包含的UIImage對象。您可以使用相同的圖像對象多次在陣中。

例如:将一系列幀添加到一個數組裡面,然後設定animation一系列屬性,如動畫時間,動畫重複次數,還是看代碼吧,直覺

NSArray *magesArray = [NSArray arrayWithObjects:
              [UIImage imageNamed:@"image1.png"],
              [UIImage imageNamed:@"image2.png"],
              [UIImage imageNamed:@"image3.png"],
              [UIImage imageNamed:@"image4.png"],
              [UIImage imageNamed:@"image5.png"],nil];

UIImageView *animationImageView = [UIImageView alloc]init];
[animationImageView initWithFrame:CGRectMake(0, 0, 131, 125)];
animationImageView.animationImages = imagesArray;//将序列幀數組賦給UIImageView的animationImages屬性
animationImageView.animationDuration = 0.25;//設定動畫時間
animationImageView.animationRepeatCount = 0;//設定動畫次數 0 表示無限
[animationImageView startAnimating];//開始播放動畫      

     但是,如果圖檔少的話也許這種方式是最快速最容易達到目的 的,但是圖檔很多的話,根據目前我做的實 驗,圖檔很多的話 這種方式程式必須會蹦,随後我會提到我們現在的實作方 式,而且動畫不能夠實作暫 停,隻有停止,項目中要求序列幀播放的時候當手輕觸(touch)播放暫停 ,松開後繼續播放 ,橫 掃(swipe)播放加 速,這一系列的需求表明了用animationImages這種方式實作已經不太現實.因為UIImageView的animation不會邊用邊釋放(當然這點僅是我自己的拙見),那就導緻了如果圖檔很多,animation 直接崩掉根本 用不了,我們實作的原理就是用NSTimer去實作apple的UIImageView animation的效果 ,其實apple應該也是用NSTimer去實作吧(猜的),用NSTimer每隔一個時間戳去設定一次image ,代碼如下

NSTimer *myAnimatedTimer = [NSTimer scheduledTimerWithTimeInterval:0.04 target:self selector:@selector(setNextImage) userInfo:nil repeats:YES];

-(void) setNextImage

{

   myAnimatedView.image = [UIImage imageNamed:[NSStringstringWithFormat:@"image%i.png",nextImage]];

}