#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *tomImageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)touchHeadOnclick:(id)sender {
[self tomAnimationWith:@"knockout" imageCount:81];
}
- (IBAction)eatBirdOnclick:(id)sender {
[self tomAnimationWith:@"eat" imageCount:40];
}
-(void) tomAnimationWith:(NSString*)imageFile imageCount:(int)imageCount {
if (self.tomImageView.isAnimating) {
return;
}
// 1首先创建图片数组
NSMutableArray *tomImages = [NSMutableArray array];
for (int i = 0; i < imageCount; i++) {
NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg", imageFile, i];
// 图片过多的时候容易造成内存溢出
// UIImage *image = [UIImage imageNamed:imageName];
NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
UIImage * image = [UIImage imageWithContentsOfFile:path];
[tomImages addObject:image];
}
// 2设置动画过程
// 2.1添加动画执行的图片数组
[self.tomImageView setAnimationImages:tomImages];
// 2.2设置动画执行的时间
[self.tomImageView setAnimationDuration:self.tomImageView.animationImages.count * 0.1];
// 2.3设置动画执行次数
[self.tomImageView setAnimationRepeatCount:1];
// 设置开始执行动画
[self.tomImageView startAnimating];
// 3在动画结束之后清空数据
//[self performSelector:@selector(clearImages) withObject:nil afterDelay:self.tomImageView.animationDuration];
[self.tomImageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tomImageView.animationDuration];
}
//- (void) clearImages {
// self.tomImageView.animationImages = nil;
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
IOS汤姆猫小练习
最新推荐文章于 2023-03-05 15:57:25 发布