功能分析
点击对应的按钮后,让汤姆猫展现对应的动画
步骤分析
搭建UI界面
监听按钮点击
点击对应的按钮后,让汤姆猫展现对应的动画
步骤分析
搭建UI界面
监听按钮点击
根据点击的按钮执行对应的动画
一.定义监听事件方法和对象
- (IBAction)drink; //喝水方法
- (IBAction)knock; //敲击方法
- (IBAction)rightFoot; //摸脚方法
- (void)runAnimationWithCount:(int)count name:(NSString *)name //动画实现
/** 这是一只显示图片的猫 */
@property (weak, nonatomic) IBOutlet UIImageView *tom;
二.实现方法
- (void)runAnimationWithCount:(int)count name:(NSString *)name
{
if (self.tom.isAnimating) return;
// 1.加载所有的动画图片
NSMutableArray *images = [NSMutableArray array];
for (int i = 0; i<count; i++) {
// 计算文件名
NSString *filename = [NSString stringWithFormat:@"%@_%02d.jpg", name, i];
// 加载图片
// imageNamed: 有缓存(传入文件名)
// UIImage *image = [UIImage imageNamed:filename];
// imageWithContentsOfFile: 没有缓存(传入文件的全路径)
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:filename ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];
// 添加图片到数组中
[images addObject:image];
}
self.tom.animationImages = images;
// 2.设置播放次数(1次)
self.tom.animationRepeatCount = 1;
// 3.设置播放时间
self.tom.animationDuration = images.count * 0.05;
[self.tom startAnimating];
// 4.动画放完1秒后清除内存
CGFloat delay = self.tom.animationDuration + 1.0;
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];
// [self performSelector:@selector(clearCache) withObject:nil afterDelay:delay];
}
代码分析:
<span style="color:#336666;">1) UIImageView</span><span style="color:#336666;">帧动画相关属性和方法:</span>
<span style="color:#336666;"></span><div>•<span style="color:#AA0D91;">@property</span>(<span style="color:#AA0D91;">nonatomic</span>,<span style="color:#AA0D91;">copy</span>) <span style="color:#5C2699;">NSArray</span>*animationImages; </div><div>Ø需要播放的序列帧图片数组(里面都是UIImage对象,会按顺序显示里面的图片)</div><div>Ø</div><div>•<span style="color:#AA0D91;">@</span><span style="color:#AA0D91;">property</span>(<span style="color:#AA0D91;">nonatomic</span>) <span style="color:#5C2699;">NSTimeInterval</span>animationDuration;</div><div>Ø帧动画的持续时间</div><div>•</div><div>•<span style="color:#AA0D91;">@</span><span style="color:#AA0D91;">property</span>(<span style="color:#AA0D91;">nonatomic</span>) <span style="color:#5C2699;">NSInteger</span> animationRepeatCount; </div><div>Ø帧动画的执行次数(默认是无限循环)</div><div>•</div><div>•- (<span style="color:#AA0D91;">void</span>)startAnimating;</div><div>Ø开始执行帧动画</div><div>•</div><div>•- (<span style="color:#AA0D91;">void</span>)stopAnimating;</div><div>Ø停止执行帧动画</div><div>Ø</div><div>•- (<span style="color:#AA0D91;">BOOL</span>)isAnimating;</div><div>Ø是否正在执行帧动画</div><div>
</div><div>2) <span style="color:#336666;">UIImage</span><span style="color:#336666;">的</span><span style="color:#336666;">2</span><span style="color:#336666;">种加载方式</span></div><div>•方式一:有缓存(图片所占用的内存会一直停留在程序中)</div><div>Ø+ (<span style="color:#5C2699;">UIImage</span>*)imageNamed:(<span style="color:#5C2699;">NSString</span>*)name;</div><div>Øname是图片的文件名</div><div>•方式二:无缓存(图片所占用的内存会在一些特定操作后被清除)</div><div>Ø+ (<span style="color:#5C2699;">UIImage</span>*)imageWithContentsOfFile:(<span style="color:#5C2699;">NSString</span> *)path</div><div>Ø- (<span style="color:#AA0D91;">id</span>)initWithContentsOfFile:(<span style="color:#5C2699;">NSString</span>*)path;</div><div>Øpath是图片的全路径</div><div>
</div><div>3) NSBundle 的使用</div><div> mainBundle :获得全部资源 </div><div>
</div><div><p class="p1">- (<span class="s1">IBAction</span>)knock {</p><p class="p2"><span class="s2"> [</span><span class="s1">self</span><span class="s2"> </span>runAnimationWithCount<span class="s2">:81 </span>name<span class="s2">:</span><span class="s3">@"knockout"</span><span class="s2">]; //实现敲击</span></p><p class="p1">}</p>
</div>
<span style="color:#336666;"></span><pre name="code" class="html">