UIImageView动画 、自定义View 和手势的简单实用 Tom 猫 全部功能实现

创建一个自定义view  Tom. h Tom.m   /*其实没啥用*/

#import <UIKit/UIKit.h>


@interface Tom : UIView


@property (nonatomic, retain) UIImageView *tom;


@end



#import "Tom.h"


@implementation Tom


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    NSLog(@"sssss");


}

在AppDelegate.m 中创建一个 VC 作为根视图

  RootViewController *rootVC = [[RootViewController alloc] init];

    self.window.rootViewController = rootVC;

    [rootVC release];

    [_window release];


主要 是 RootViewController.m 的实现部分


#import "RootViewController.h"

#import "Tom.h"


@interface RootViewController ()


@property (nonatomic, retain) UIImageView *tom;

@property (nonatomic, assign) CGFloat width;

@property (nonatomic, assign) CGFloat height;



@end


@implementation RootViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    

    self.width = self.view.frame.size.width;

    self.height = self.view.frame.size.height;



    UIImage *image = [UIImage imageNamed:@"angry_00.jpg"];

    self.tom = [[UIImageView alloc] initWithImage:image];

    self.tom.frame = [UIScreen mainScreen].bounds;

    [self.view addSubview:self.tom];

    [_tom release];

    NSArray *action = [NSArray arrayWithObjects:@"cymbal", @"drink", @"eat", @"fart", @"pie", @"scratch", nil];

    NSArray *tagNum = [NSArray arrayWithObjects:@"12", @"80", @"39", @"27" , @"23", @"55", nil];

    /* 循环生成button */

    for (int i = 0; i < 2; i++) {

        for (int j = 0 ; j < 3; j++) {

            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

            [button setImage:[UIImage imageNamed:action[i + 2 * j]] forState:UIControlStateNormal];

            button.frame = CGRectMake(self.width / 15 + (i * self.width * 11 / 15), self.height * 3 / 5 + (j * self.height / 10) , 50, 50);

            button.tag = [tagNum[i + 2 * j] integerValue];

            [button setTitle:action[i + 2 * j] forState:UIControlStateNormal];

            [button addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];

            [self.view addSubview:button];

        }

    }

    /* 肚子 */

    Tom *label = [[Tom alloc] initWithFrame:CGRectMake(self.width/3, self.height*6/10, self.width/3, self.width/3)];

    label.backgroundColor = [UIColor lightGrayColor];

    label.alpha = 0.1;

    [self.view addSubview:label];

    [label release];

    

    /* 尾巴 */

    Tom *tail = [[Tom alloc] initWithFrame:CGRectMake(self.width * 13 / 20, self.height * 3.7 / 5, 60, 100)];

    tail.backgroundColor = [UIColor lightGrayColor];

    tail.alpha = 0.1f;

    [self.view addSubview:tail];

    [tail release];

    

    /* 左脚 */

    Tom *footLife = [[Tom alloc] initWithFrame:CGRectMake(self.width / 2 - self.width * 2 / 10, self.height * 9 / 10, 70, 50)];

    footLife.backgroundColor = [UIColor lightGrayColor];

    footLife.alpha = 0.1f;

    [self.view addSubview:footLife];

    [footLife release];

    

    /* 右脚 */

    Tom *footRight = [[Tom alloc] initWithFrame:CGRectMake(self.width / 2, self.height * 9 / 10, 70, 50)];

    footRight.backgroundColor = [UIColor lightGrayColor];

    footRight.alpha = 0.1f;

    [self.view addSubview:footRight];

    [footRight release];

    

    /* */

    Tom *knockout = [[Tom alloc] initWithFrame:CGRectMake(self.width / 3, self.height / 6, 130, 50)];

    knockout.backgroundColor = [UIColor lightGrayColor];

    knockout.alpha = 0.1f;

    [self.view addSubview:knockout];

    [knockout release];

    

#pragma mark ** 轻拍手势

    /* 1.创建对象 */

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1:)];

    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap2:)];

    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap3:)];

#pragma mark ** 长按手势

    UILongPressGestureRecognizer *lTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lTap:)];

    /* 2.设置相关的属性 */

    tap.numberOfTapsRequired = 1;

    tap.numberOfTouchesRequired = 1;

    

    lTap.minimumPressDuration = 0.5f;

    lTap.numberOfTouchesRequired = 1;/* 手指数 */

    lTap.numberOfTapsRequired = 0;/* 长按前轻拍次数 */


    [label addGestureRecognizer:tap];

    [tail addGestureRecognizer:tap1];

    [footLife addGestureRecognizer:tap2];

    [footRight addGestureRecognizer:tap3];

    [knockout addGestureRecognizer:lTap];

    

    /* 内存管理 */

    [tap release];

    [tap1 release];

    [tap2 release];

    [tap3 release];

    [lTap release];

    

    

}


- (void)tap:(UITapGestureRecognizer *)tap {

    NSLog(@"轻拍");

    //NSLog(@"%s", __func__);

    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:0];

    for (int i = 0; i < 34 ; i++) {

        /* 获取照片名字 */

        NSString *imageName = [NSString stringWithFormat:@"stomach_%02d.jpg", i];

        /* 根据名字获取路径 */

        NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];

        /* 根据路径获取照片装入数组 */

        UIImage *image = [UIImage imageWithContentsOfFile:path];

        arrayM[i] = image;

    }

    NSLog(@"%@", arrayM);

    /* 开始执行动画 */

    [self.tom setAnimationImages:arrayM];

    [self.tom setAnimationRepeatCount:1];

    /* 设置执行时间 */

    self.tom.animationDuration = arrayM.count * 0.075;

    [self.tom startAnimating];

    /* 结束动画后,清理动画数组 */

    [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];

}

/* 尾巴 */

- (void)tap1:(UITapGestureRecognizer *)tap {

    NSLog(@"轻拍");

    //NSLog(@"%s", __func__);

    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:0];

    for (int i = 0; i < 25 ; i++) {

        /* 获取照片名字 */

        NSString *imageName = [NSString stringWithFormat:@"angry_%02d.jpg", i];

        /* 根据名字获取路径 */

        NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];

        /* 根据路径获取照片装入数组 */

        UIImage *image = [UIImage imageWithContentsOfFile:path];

        arrayM[i] = image;

    }

    NSLog(@"%@", arrayM);

    /* 开始执行动画 */

    [self.tom setAnimationImages:arrayM];

    [self.tom setAnimationRepeatCount:1];

    /* 设置执行时间 */

    self.tom.animationDuration = arrayM.count * 0.075;

    [self.tom startAnimating];

    /* 结束动画后,清理动画数组 */

    [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];

}

/* 左脚 */

- (void)tap2:(UITapGestureRecognizer *)tap {

    NSLog(@"轻拍");

    //NSLog(@"%s", __func__);

    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:0];

    for (int i = 0; i < 29 ; i++) {

        /* 获取照片名字 */

        NSString *imageName = [NSString stringWithFormat:@"footRight_%02d.jpg", i];

        /* 根据名字获取路径 */

        NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];

        /* 根据路径获取照片装入数组 */

        UIImage *image = [UIImage imageWithContentsOfFile:path];

        arrayM[i] = image;

    }

    NSLog(@"%@", arrayM);

    /* 开始执行动画 */

    [self.tom setAnimationImages:arrayM];

    [self.tom setAnimationRepeatCount:1];

    /* 设置执行时间 */

    self.tom.animationDuration = arrayM.count * 0.075;

    [self.tom startAnimating];

    /* 结束动画后,清理动画数组 */

    [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];

}

/* 右脚 */

- (void)tap3:(UITapGestureRecognizer *)tap {

    NSLog(@"轻拍");

    //NSLog(@"%s", __func__);

    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:0];

    for (int i = 0; i < 29 ; i++) {

        /* 获取照片名字 */

        NSString *imageName = [NSString stringWithFormat:@"footLeft_%02d.jpg", i];

        /* 根据名字获取路径 */

        NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];

        /* 根据路径获取照片装入数组 */

        UIImage *image = [UIImage imageWithContentsOfFile:path];

        arrayM[i] = image;

    }

    NSLog(@"%@", arrayM);

    /* 开始执行动画 */

    [self.tom setAnimationImages:arrayM];

    [self.tom setAnimationRepeatCount:1];

    /* 设置执行时间 */

    self.tom.animationDuration = arrayM.count * 0.075;

    [self.tom startAnimating];

    /* 结束动画后,清理动画数组 */

    [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];

}


/* */


- (void)lTap:(UILongPressGestureRecognizer *)lTap {

    /* 对于长按手势可能会触发两次 解决办法:判断手势状态 */

    if (lTap.state == UIGestureRecognizerStateBegan) {

        NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:0];

        for (int i = 0; i < 80 ; i++) {

            /* 获取照片名字 */

            NSString *imageName = [NSString stringWithFormat:@"knockout_%02d.jpg", i];

            /* 根据名字获取路径 */

            NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];

            /* 根据路径获取照片装入数组 */

            UIImage *image = [UIImage imageWithContentsOfFile:path];

            arrayM[i] = image;

        }

        NSLog(@"%@", arrayM);

        /* 开始执行动画 */

        [self.tom setAnimationImages:arrayM];

        [self.tom setAnimationRepeatCount:1];

        /* 设置执行时间 */

        self.tom.animationDuration = arrayM.count * 0.075;

        [self.tom startAnimating];

        /* 结束动画后,清理动画数组 */

        [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];

    }

    NSLog(@"%s", __func__);

}



/* button 点击事件 */

- (void)tomAction:(UIButton *)button {

    NSLog(@"asdsadas");

    [self tomAnimationWithName:button.currentTitle count:button.tag];

}


/*  触发事件  */

-(void) tomAnimationWithName:(NSString *)name count:(NSInteger )count {

    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:0];

    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];

        arrayM[i] = image;

    }

    /* 开始执行动画 */

    [self.tom setAnimationImages:arrayM];

    [self.tom setAnimationRepeatCount:1];

    /* 设置执行时间 */

    self.tom.animationDuration = arrayM.count * 0.075;

    [self.tom startAnimating];

    /* 结束动画后,清理动画数组 */

    [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值