iOS- 利用UIImageView自己整了個不會說話的湯姆貓


 

1.實現思路             

 

先說說我實現它的主要思路,很簡單,主要利用UIImageView連續動畫播放,和按鈕的點擊事件,就可以完成了這么一個簡單的不會說話的湯姆貓。

 

2.實現細節             

 

2.1.加載本地字典里保存的本地圖片名       

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

 

2.2.抽取動畫連續播放的方法出來         

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

 

2.3.監聽按鈕的點擊,實現圖片的連續播放形成動畫

 

 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.cnblogs.com/qingche/
 本文版權歸作者和博客園共有,歡迎轉載,但必須保留此段聲明,且在文章頁面明顯位置給出原文連接。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM