您的位置:首页 > 移动开发 > IOS开发

iOS 自制汤姆猫

2015-10-06 16:00 357 查看
效果图如下:



#import "twoViewController.h"

@interface twoViewController ()
{
UIImageView *imageView;

UIImageView *animationView_stomach;
NSMutableArray *imageList_stomach;

UIImageView *animationView_footleft;
NSMutableArray *imageList_footleft;

UIImageView *animationView_footright;
NSMutableArray *imageList_footright;
}
@end

@implementation twoViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.view.backgroundColor = [UIColor grayColor];

imageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
imageView.image = [UIImage imageNamed:@"fart_00.jpg"];
[self.view addSubview:imageView];

// 胃部
UIButton *b_stomach = [UIButton buttonWithType:UIButtonTypeCustom];
b_stomach.frame = CGRectMake(150, 450, 80, 100);
[b_stomach addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
b_stomach.tag = 100;
[self.view addSubview:b_stomach];

// 左脚
UIButton *b_footleft = [UIButton buttonWithType:UIButtonTypeCustom];
b_footleft.frame = CGRectMake(190, 600, 50, 50);
[b_footleft addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
b_footleft.tag = 101;
[self.view addSubview:b_footleft];

// 右脚
UIButton *b_footright = [UIButton buttonWithType:UIButtonTypeCustom];
b_footright.frame = CGRectMake(130, 600, 50, 50);
[b_footright addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
b_footright.tag = 102;
[self.view addSubview:b_footright];

NSArray *arr = @[@"scratch",@"pie",@"drink",@"fart",@"eat",@"cymbal"];
// 左边三个按钮动作
for (int i = 0; i<3; i++) {
UIButton *bleft = [UIButton buttonWithType:UIButtonTypeCustom];
bleft.frame = CGRectMake(0, 300+(50+10)*i, 50, 50);
bleft.tag = i+1;
[bleft setBackgroundImage:[UIImage imageNamed:arr[i]] forState:UIControlStateNormal];
[self.view addSubview:bleft];
[bleft addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
}
// 右边三个按钮动作
for (int j = 0; j<3; j++) {
UIButton *bright = [UIButton buttonWithType:UIButtonTypeCustom];
bright.frame = CGRectMake(375-50, 300+(50+10)*j, 50, 50);
bright.tag = j+10;
[bright setBackgroundImage:[UIImage imageNamed:arr[3+j]] forState:UIControlStateNormal];
[self.view addSubview:bright];
[bright addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
}
}

- (void)loadAnimationWithCount:(int)count name:(NSString *)name
{
if ([imageView isAnimating]) {
return;
}
//    把图片里面的数据 加载到内存 然后再从内存里面 获得图片数据
NSMutableArray *images = [NSMutableArray array];
//    如果读取高质量的图片 可以采取以下方式
for (int i =0; i<count; i++) {
NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg",name,i];

NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];
[images addObject:image];
}

imageView.animationImages = images;
imageView.animationRepeatCount = 1;
imageView.animationDuration = images.count*0.05;

[imageView startAnimating];

CGFloat delay = imageView.animationDuration +0.1;
[imageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];

}

- (void)leftAction:(UIButton *)sender
{
switch (sender.tag) {
case 1:
[self loadAnimationWithCount:55 name:@"scratch"];
break;
case 2:
[self loadAnimationWithCount:23 name:@"pie"];
break;
case 3:
[self loadAnimationWithCount:80 name:@"drink"];
break;
case 100:
[self loadAnimationWithCount:33 name:@"stomach"];
break;
case 101:
[self loadAnimationWithCount:29 name:@"foot_left"];
break;
case 102:
[self loadAnimationWithCount:29 name:@"foot_right"];
break;

default:
break;
}
}

- (void)rightAction:(UIButton *)sender
{
switch (sender.tag) {
case 10:
[self loadAnimationWithCount:27 name:@"fart"];
break;
case 11:
[self loadAnimationWithCount:39 name:@"eat"];
break;
case 12:
[self loadAnimationWithCount:12 name:@"cymbal"];
break;

default:
break;
}
}


这个汤姆猫主要是利用button来控制图片而已,不过我们的图片是所有的图片和在一起的,看起来就像是一连串的动作。本人知识有限,如有其它建议或者意见,必会虚心 改进。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: