TOM猫的简单实现

#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.

}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值