uiimageView连续帧动画

  1 //
  2 
  3 //  MJViewController.m
  4 
  5 //  05-汤姆猫
  6 
  7 //
  8 
  9 //  Created by apple on 14-3-24.
 10 
 11 //  Copyright (c) 2014年 itcast. All rights reserved.
 12 
 13 //
 14 
 15  
 16 
 17 #import "MJViewController.h"
 18 
 19  
 20 
 21 @interface MJViewController ()
 22 
 23 - (IBAction)drink;
 24 
 25 - (IBAction)knock;
 26 
 27 - (IBAction)rightFoot;
 28 
 29  
 30 
 31 /** 这是一只显示图片的猫 */
 32 
 33 @property (weak, nonatomic) IBOutlet UIImageView *tom;
 34 
 35  
 36 
 37 @end
 38 
 39  
 40 
 41 @implementation MJViewController
 42 
 43 /** 播放动画 */
 44 
 45 - (void)runAnimationWithCount:(int)count name:(NSString *)name
 46 
 47 {
 48 
 49     if (self.tom.isAnimating) return;
 50 
 51     
 52 
 53     // 1.加载所有的动画图片
 54 
 55     NSMutableArray *images = [NSMutableArray array];
 56 
 57     
 58 
 59     for (int i = 0; i<count; i++) {
 60 
 61         // 计算文件名
 62 
 63         NSString *filename = [NSString stringWithFormat:@"%@_%02d.jpg", name, i];
 64 
 65         // 加载图片
 66 
 67         
 68 
 69         // imageNamed: 有缓存(传入文件名)
 70 
 71 //        UIImage *image = [UIImage imageNamed:filename];
 72 
 73         
 74 
 75         // imageWithContentsOfFile: 没有缓存(传入文件的全路径)
 76 
 77         NSBundle *bundle = [NSBundle mainBundle];
 78 
 79         NSString *path = [bundle pathForResource:filename ofType:nil];
 80 
 81         UIImage *image = [UIImage imageWithContentsOfFile:path];
 82 
 83         
 84 
 85         // 添加图片到数组中
 86 
 87         [images addObject:image];
 88 
 89     }
 90 
 91     self.tom.animationImages = images;
 92 
 93     
 94 
 95     // 2.设置播放次数(1次)
 96 
 97     self.tom.animationRepeatCount = 1;
 98 
 99     
100 
101     // 3.设置播放时间
102 
103     self.tom.animationDuration = images.count * 0.05;
104 
105     
106 
107     [self.tom startAnimating];
108 
109     
110 
111     // 4.动画放完1秒后清除内存
112 
113     CGFloat delay = self.tom.animationDuration + 1.0;
114 
115     [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];
116 
117 //    [self performSelector:@selector(clearCache) withObject:nil afterDelay:delay];
118 
119 }
120 
121  
122 
123 //- (void)clearCache
124 
125 //{
126 
127 ////    self.tom.animationImages = nil;
128 
129 //    
130 
131 //    [self.tom setAnimationImages:nil];
132 
133 //}
134 
135  
136 
137 - (IBAction)drink {
138 
139     [self runAnimationWithCount:81 name:@"drink"];
140 
141     
142 
143 //    if (self.tom.isAnimating) return;
144 
145 //    
146 
147 //    // 1.加载所有的动画图片
148 
149 //    NSMutableArray *images = [NSMutableArray array];
150 
151 //    
152 
153 //    for (int i = 0; i<81; i++) {
154 
155 //        // 计算文件名
156 
157 //        NSString *filename = [NSString stringWithFormat:@"drink_%02d.jpg", i];
158 
159 //        // 加载图片
160 
161 //        UIImage *image = [UIImage imageNamed:filename];
162 
163 //        // 添加图片到数组中
164 
165 //        [images addObject:image];
166 
167 //    }
168 
169 //    self.tom.animationImages = images;
170 
171 //    
172 
173 //    // 2.设置播放次数(1次)
174 
175 //    self.tom.animationRepeatCount = 1;
176 
177 //    
178 
179 //    // 3.设置播放时间
180 
181 //    self.tom.animationDuration = images.count * 0.05;
182 
183 //    
184 
185 //    [self.tom startAnimating];
186 
187 }
188 
189  
190 
191 - (IBAction)knock {
192 
193     [self runAnimationWithCount:81 name:@"knockout"];
194 
195 }
196 
197  
198 
199 - (IBAction)rightFoot {
200 
201     [self runAnimationWithCount:30 name:@"footRight"];
202 
203 }
204 
205 @end
原文地址:https://www.cnblogs.com/WiliamF/p/6264857.html