标签:
//
// ViewController.m
// TomCat
//
// Created by 黄松凯 on 15/2/17.
// Copyright (c) 2015年 SK. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
- (IBAction)drink;
- (IBAction)head;
- (IBAction)cymbal;
- (IBAction)fart;
- (IBAction)scratch;
- (IBAction)eat;
- (IBAction)pie;
@property (weak, nonatomic) IBOutlet UIImageView *tom;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
//将方法进行封装,可以
-(void)runAnimationWithCount:(int)count name:(NSString *)name
{
if(self.tom.isAnimating)//如果正在喝牛奶,则不要打断
return;
//加载所有的动画图片
NSMutableArray *images=[NSMutableArray array];
for(int i=0;i<count;i++)
{
//文件名
NSString *filename=[NSString stringWithFormat:@"%@_%02d.jpg",name,i];
//加载图片(这种方法有缓存,不利用内存优化)
// UIImage *image=[UIImage imageNamed:filename];
//添加图片到数组中
//[images addObject:image];
NSBundle *bundle=[NSBundle mainBundle];
NSString *path=[bundle pathForResource:filename ofType:nil];
UIImage *image=[UIImage imageWithContentsOfFile:path];
[images addObject:image];
}
self.tom.animationImages=images;
//设置播放次数
self.tom.animationRepeatCount=1;
//设置播放时间
self.tom.animationDuration=2.0;
//开始播放
[self.tom startAnimating];
CGFloat delay=self.tom.animationDuration+1;
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];
}
- (IBAction)drink
{
[self runAnimationWithCount:81 name:@"drink"];
}
- (IBAction)head {
[self runAnimationWithCount:81 name:@"knockout"];
}
- (IBAction)cymbal {
[self runAnimationWithCount:13 name:@"cymbal"];
}
- (IBAction)fart {
[self runAnimationWithCount:28 name:@"fart"];
}
- (IBAction)scratch {
[self runAnimationWithCount:56 name:@"scratch"];
}
- (IBAction)eat {
[self runAnimationWithCount:40 name:@"eat"];
}
- (IBAction)pie {
[self runAnimationWithCount:24 name:@"pie"];
}
@end
标签:
原文地址:http://www.cnblogs.com/SKuncle/p/4295347.html