动画
1、将所有的图片名存到一个数组当中,也可以写成一个方法调用
imageList = [NSMutableArray array];
for (int i = 1; i < 11; i ++) {
NSString *imageName = [NSString stringWithFormat:@"gif图片2-%d(被拖移).tiff", i];
[imageList addObject:[UIImage imageNamed:imageName]];
}
注:当调试的时候,打印数组的值是nil或null,意味着没有初始化对象或者有被赋值为nil或null(数组对象就会被销毁)的情况
2、添加图片及其坐标
UIImage *image = [UIImage imageNamed:@"gif图片2-1(被拖移).tiff"];
animationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width/10, image.size.height/10)];
3、设置UIImageView播放的动态图数组
animationView.animationImages = imageList;
4、播放一组动画需要的时间
animationView.animationDuration = 1;
5、播放次数
animationView.animationRepeatCount = -1;
6、加载
[self.view addSubview:animationView];
7、让动画开始
[animationView startAnimating];
8、让动画结束
[animationView stopAnimating];
9、判断动画是否正在播放
animationView.isAnimating = YES;
10、设置动画移动坐标到指定位置所需要的时间
写法一:
[UIView animateWithDuration:5 animations:^{
animationView.frame = CGRectMake(100, 200, image.size.width*2, image.size.height * 2);
}];
写法二:在移动坐标的同时,还改变动画的宽和高
[UIView animateWithDuration:20 animations:^{
animationView.frame = CGRectMake(0, 400, image.size.width * 2, image.size.height * 2);
} completion:^(BOOL finished) {
[UIView animateWithDuration:20 animations:^{
animationView.frame = CGRectMake(0, 0, image.size.width/10, image.size.height/10);
}];
}];
11、设置播放、暂停按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10, 20, 50, 35);
[button setTitle:@"播放" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitle:@"暂停" forState:UIControlStateSelected];
[button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
注:
play:为点击播放、暂停按钮时触发的事件,具体内容如下
- (void)play:(UIButton *)sender {
if (sender.selected != YES) {
// 播放动画,重置按钮的title
sender.selected = YES;
[animationView startAnimating];
[self moveAnimation];
}else{
sender.selected = NO;
[animationView stopAnimating];
}
}
例:汤姆猫动画的简单实现
#import "ViewController.h"
@interface ViewController ()
{
UIImageView *animationView;
NSMutableArray *imageList;
UIButton *buttun;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *backgroundImage = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
backgroundImage.image = [UIImage imageNamed:@"angry_00"];
[self.view addSubview:backgroundImage];
animationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds))];
[self.view addSubview:animationView];
// 右脚
[self creatButtun:130 origin:607 width:55 height:35 tag:100];
// 左脚
[self creatButtun:186 origin:607 width:55 height:35 tag:101];
// 生气
[self creatButtun:158 origin:217 width:70 height:38 tag:102];
// 敲锣
[self creatButtun:230 origin:356 width:50 height:66 tag:103];
// 喝饮料
[self creatButtun:110 origin:256 width:84 height:60 tag:104];
// 吃东西
[self creatButtun:195 origin:256 width:80 height:60 tag:105];
// 放屁
[self creatButtun:252 origin:500 width:36 height:110 tag:106];
// 倒地
[self creatButtun:145 origin:115 width:82 height:46 tag:107];
// 拍屏幕
[self creatButtun:66 origin:111 width:54 height:80 tag:108];
// 划屏幕
[self creatButtun:97 origin:374 width:40 height:60 tag:109];
// 胃痛
[self creatButtun:141 origin:370 width:88 height:210 tag:110];
}
- (void)creatView:(NSString *)name viewNumber:(int)number animationViewData:(int)animationDuration {
imageList = [NSMutableArray array];
for (int i = 0; i < number; i ++) {
NSString *imageName = [[NSString alloc]init];
imageName = [name stringByAppendingFormat:@"_%02d", i];
[imageList addObject:[UIImage imageNamed:imageName]];
}
animationView.animationImages = imageList;
animationView.animationDuration = animationDuration;
animationView.animationRepeatCount = 1;
[animationView startAnimating];
}
- (void)creatButtun:(int)origin_x origin:(int)origin_y width:(int)x height:(int)y tag:(int)tag {
buttun = [UIButton buttonWithType:UIButtonTypeCustom];
buttun.frame = CGRectMake(origin_x, origin_y, x, y);
buttun.tag = tag;
// buttun.backgroundColor = [UIColor whiteColor];
[buttun addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttun];
}
- (void)play:(UIButton *)sender {
if (animationView.isAnimating != YES) {
sender.selected = NO;
[animationView stopAnimating];
}
if (sender.selected != YES) {
sender.selected = YES;
switch (sender.tag) {
// 右脚
case 100:
[self creatView:@"foot_right" viewNumber:29 animationViewData:5];
break;
// 左脚
case 101:
[self creatView:@"foot_left" viewNumber:29 animationViewData:5];
break;
// 生气
case 102:
[self creatView:@"angry" viewNumber:25 animationViewData:4.5];
break;
// 敲锣
case 103:
[self creatView:@"cymbal" viewNumber:12 animationViewData:2];
break;
// 喝饮料
case 104:
[self creatView:@"drink" viewNumber:80 animationViewData:15];
break;
// 吃东西
case 105:
[self creatView:@"eat" viewNumber:39 animationViewData:8];
break;
// 放屁
case 106:
[self creatView:@"fart" viewNumber:27 animationViewData:5];
break;
// 倒地
case 107:
[self creatView:@"knockout" viewNumber:80 animationViewData:15];
break;
// 拍屏幕
case 108:
[self creatView:@"pie" viewNumber:23 animationViewData:5];
break;
// 划屏幕
case 109:
[self creatView:@"scratch" viewNumber:55 animationViewData:10];
break;
// 胃痛
case 110:
[self creatView:@"stomach" viewNumber:33 animationViewData:5.5];
break;
default:
break;
}
}else{
sender.selected = NO;
[animationView stopAnimating];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end