#import "MyViewController.h"
@interface MyViewController ()
@end
@implementation MyViewController
- (void)dealloc
{
[_tom release];
[_array release];
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_array = [@[@{@"count" : @28, @"name" : @"fart"}, @{@"count" : @13, @"name" : @"cymbal"}, @{@"count" : @80, @"name" : @"drink"}, @{@"count" : @39, @"name" : @"eat"}, @{@"count" : @24, @"name" : @"pie"}, @{@"count" : @56, @"name" : @"scratch"}, @{@"count": @30 ,@"name" : @"foot_left"}, @{@"count" : @30, @"name": @"foot_right"}, @{@"count" : @34, @"name" : @"stomach"}, @{@"count" : @81, @"name" : @"knockout"}, @{@"count" : @26, @"name" : @"angry"}] retain];
}
return self;
}
- (void)loadView
{
[super loadView];
[self creatTom];
[self creatButton:CGRectMake(20, 420, 60, 60) image:[UIImage imageNamed:@"fart.png"] tag:101];
[self creatButton:CGRectMake(20, 500, 60, 60) image:[UIImage imageNamed:@"cynbals.png"] tag:102];
[self creatButton:CGRectMake(20, 580, 60, 60) image:[UIImage imageNamed:@"milk.png"] tag:103];
[self creatButton:CGRectMake(300, 420, 60, 60) image:[UIImage imageNamed:@"larry.png"] tag:104];
[self creatButton:CGRectMake(300, 500, 60, 60) image:[UIImage imageNamed:@"pie.png"] tag:105];
[self creatButton:CGRectMake(300, 580, 60, 60) image:[UIImage imageNamed:@"pawn.png"] tag:106];
[self creatBodyButton:CGRectMake(190, 590, 50, 50) tag:107];
[self creatBodyButton:CGRectMake(120, 590, 50, 50) tag:108];
[self creatBodyButton:CGRectMake(130, 420, 120, 120) tag:109];
[self creatBodyButton:CGRectMake(100, 130, 180, 180) tag:110];
[self creatBodyButton:CGRectMake(250, 550, 40, 80) tag:111];
}
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *backImage = [UIImage imageNamed:@"angry_00.jpg"];
_tom.image = backImage;
// Do any additional setup after loading the view.
}
#pragma mark - 创建TOM背景
- (void)creatTom
{
_tom = [[UIImageView alloc] initWithFrame:self.view.frame];
[self.view addSubview:_tom];
}
#pragma mark - 创建身上button
- (void)creatBodyButton:(CGRect)rect tag:(NSInteger)tag
{
_tom.userInteractionEnabled = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = rect;
// button.backgroundColor = [UIColor cyanColor];
button.tag = tag;
[_tom addSubview:button];
[button addTarget:self action:@selector(doClick:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 创建button
- (void)creatButton:(CGRect)rect image:(UIImage *)image tag:(NSInteger)tag
{
_tom.userInteractionEnabled = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = rect;
button.backgroundColor = [UIColor clearColor];
[button setImage:image forState:UIControlStateNormal];
button.tag = tag;
[_tom addSubview:button];
[button addTarget:self action:@selector(doClick:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - doClick
- (void)doClick:(id)sender
{
NSLog(@"%s, %d", __func__, __LINE__);
UIButton *aButton = (UIButton *)sender;
NSLog(@"%s, %d", __func__, __LINE__);
NSDictionary *dic = [_array objectAtIndex:aButton.tag - 101];
NSLog(@"%@", dic);
int count = [dic[@"count"] intValue];
NSString *name = dic[@"name"];
[self playAnimation:count name:name];
}
#pragma mark - 添加动画
- (void)playAnimation:(int)count name:(NSString *)name
{
NSMutableArray *images = [NSMutableArray arrayWithCapacity:count];
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 alloc] initWithContentsOfFile:path];
[images addObject:image];
[image release];
}
_tom.animationImages = images;
_tom.animationDuration = 0.1 * count;
_tom.animationRepeatCount = 1;
[_tom startAnimating];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}