1. 程式人生 > >iOS開發筆記--UIImageView的屬性之animationImages詳解

iOS開發筆記--UIImageView的屬性之animationImages詳解

 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]];
}

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