[iOS基础控件 - 3.4] 汤姆猫
1 self.tom.animationImages = images; // 存储了多张组成动画的图片 2 3 [self.tom setAnimationRepeatCount:1]; // 默认0是无限次 4 [self.tom setAnimationDuration: images.count/FramesCount]; 5 [self.tom startAnimating];
1 // imageNamed: 有缓存 2 // UIImage *image = [UIImage imageNamed:fileName]; 3 4 // 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; 10 11 @end 12 13 @implementation ViewController 14 15 - (void)viewDidLoad { 16 [super viewDidLoad]; 17 // Do any additional setup after loading the view, typically from a nib. 18 19 } 20 21 - (void)didReceiveMemoryWarning { 22 [super didReceiveMemoryWarning]; 23 // Dispose of any resources that can be recreated. 24 } 25 26 /** 点击牛奶按钮 */ 27 - (IBAction)drink { 28 [self runAnimationWithName:@"drink" andCount:80]; 29 } 30 31 /** 点击头部 */ 32 // 实质是在头部放置了一个不带文字的透明按钮 33 - (IBAction)knockHead { 34 [self runAnimationWithName:@"knockout" andCount:80]; 35 } 36 37 /** 运行相应动画 */ 38 - (void) runAnimationWithName:(NSString *) animationName andCount:(int) count { 39 if (self.tom.isAnimating) return; 40 41 NSMutableArray *images = [NSMutableArray array]; 42 for (int i=0; i <= count; i++) { 43 NSString *fileName = [NSString stringWithFormat:@"%@_%02d.jpg", animationName, i]; 44 45 // imageNamed: 有缓存 46 // UIImage *image = [UIImage imageNamed:fileName]; 47 48 // imageWithContentOfFile: 没有缓存(传入文件的全路径) 49 NSBundle *bundle = [NSBundle mainBundle]; 50 NSString *path = [bundle pathForResource:fileName ofType:nil]; 51 UIImage *image = [UIImage imageWithContentsOfFile:path]; 52 53 [images addObject:image]; 54 } 55 56 self.tom.animationImages = images; // 存储了多张组成动画的图片 57 58 [self.tom setAnimationRepeatCount:1]; // 默认0是无限次 59 [self.tom setAnimationDuration: images.count/FramesCount]; 60 [self.tom startAnimating]; 61 62 [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration + 1]; 63 } 64 65 66 @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。