1 self.tom.animationImages = images; // 存储了多张组成动画的图片23 [self.tom setAnimationRepeatCount:1]; // 默认0是无限次4 [self.tom setAnimationDuration: images.count/FramesCount];5 [self.tom startAnimating];
1 // imageNamed: 有缓存2 // UIImage *image = [UIImage imageNamed:fileName];34 // imageWithContentOfFile: 没有缓存(传入文件的全路径)5 NSBundle *bundle = [NSBundle mainBundle];6 NSString *path = [bundle pathForResource:fileName ofType:nil];7 UIImage *image = [UIImage imageWithContentsOfFile:path];
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration + 1];
1 #import "ViewController.h" 2 3 #define FramesCount 24 // 动画帧数/秒 4 5 @interface ViewController () 6 @PRoperty (weak, nonatomic) IBOutlet UIImageView *tom; 7 8 - (IBAction)drink; 9 - (IBAction)knockHead;1011 @end1213 @implementation ViewController1415 - (void)viewDidLoad {16 [super viewDidLoad];17 // Do any additional setup after loading the view, typically from a nib.1819 }2021 - (void)didReceiveMemoryWarning {22 [super didReceiveMemoryWarning];23 // Dispose of any resources that can be recreated.24 }2526 /** 点击牛奶按钮 */27 - (IBAction)drink {28 [self runAnimationWithName:@"drink" andCount:80];29 }3031 /** 点击头部 */32 // 实质是在头部放置了一个不带文字的透明按钮33 - (IBAction)knockHead {34 [self runAnimationWithName:@"knockout" andCount:80];35 }3637 /** 运行相应动画 */38 - (void) runAnimationWithName:(NSString *) animationName andCount:(int) count {39 if (self.tom.isAnimating) return;4041 NSMutableArray *images = [NSMutableArray array];42 for (int i=0; i <= count; i++) {43 NSString *fileName = [NSString stringWithFormat:@"%@_%02d.jpg", animationName, i];4445 // imageNamed: 有缓存46 // UIImage *image = [UIImage imageNamed:fileName];4748 // imageWithContentOfFile: 没有缓存(传入文件的全路径)49 NSBundle *bundle = [NSBundle mainBundle];50 NSString *path = [bundle pathForResource:fileName ofType:nil];51 UIImage *image = [UIImage imageWithContentsOfFile:path];5253 [images addObject:image];54 }5556 self.tom.animationImages = images; // 存储了多张组成动画的图片5758 [self.tom setAnimationRepeatCount:1]; // 默认0是无限次59 [self.tom setAnimationDuration: images.count/FramesCount];60 [self.tom startAnimating];6162 [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration + 1];63 }646566 @end
新闻热点
疑难解答