

先說說我實現它的主要思路,很簡單,主要利用UIImageView連續動畫播放,和按鈕的點擊事件,就可以完成了這么一個簡單的不會說話的湯姆貓。
@PRoperty (weak, nonatomic) IBOutletUIImageView *tom;
NSDictionary *_dict; // 保存所有圖片的個數
1 // 1.獲得tom.plist的全路徑2 NSBundle *bundle = [NSBundle mainBundle];3 NSString *path = [bundle pathForResource:@"tom" ofType:@"plist"];4 5 // 2.根據文件路徑加載字典6 _dict = [NSDictionary dictionaryWithContentsOfFile:path];
1.有緩存(無法釋放,參數傳的是文件名)
[UIImage imageNamed:@""];
2.無緩存(用完就會釋放,參數傳的是全路徑)
[[UIImage alloc] initWithContentsOfFile:];
// 1.創建可變數組 NSMutableArray *images = [NSMutableArray array]; // 2.添加圖片 for (int i = 0; i<count; i++) { // 圖片名 NSString *name = [NSString stringWithFormat:@"%@_%02d.jpg", filename, i]; // 全路徑 NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil]; // 加載圖片(緩存)// UIImage *img = [UIImage imageNamed:name]; // 沒有緩存 UIImage *img = [[UIImage alloc] initWithContentsOfFile:path]; [images addObject:img]; } // 3.設置動畫圖片(有順序) _tom.animationImages = images;// 序列幀動畫 // 4.只播放一次 _tom.animationRepeatCount = 1; // 5.設置動畫的持續時間 _tom.animationDuration = 0.1 * count; // 5.開始動畫 [_tom startAnimating];
1 #pragma mark 監聽所有的按鈕點擊 2 - (IBAction)btnClick:(UIButton *)sender { 3 // 1.如果tom正在播放動畫,直接返回 4 if (_tom.isAnimating) return; 5 6 // 2.取出按鈕文字 7 NSString *title = [sender titleForState:UIControlStateNormal]; 8 9 // 3.獲得圖片數量10 int count = [_dict[title] intValue];11 12 // 4.播放動畫13 [self playAnim:count filename:title];14 }
作者: 清澈Saup
出處: http://www.survivalescaperooms.com/qingche/
本文版權歸作者和博客園共有,歡迎轉載,但必須保留此段聲明,且在文章頁面明顯位置給出原文連接。
新聞熱點
疑難解答