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

来源:互联网 发布:淘宝五金店铺代理 编辑:程序博客网 时间:2024/05/16 17:26

创建一个自定义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 = [[RootViewControlleralloc] init];

    self.window.rootViewController = rootVC;

    [rootVC release];

    [_windowrelease];


主要 是 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 {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    

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

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



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

    self.tom = [[UIImageViewalloc] initWithImage:image];

    self.tom.frame = [UIScreenmainScreen].bounds;

    [self.viewaddSubview:self.tom];

    [_tomrelease];

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

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

    /* 循环生成button */

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

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

            UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];

            [button setImage:[UIImageimageNamed: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:selfaction:@selector(tomAction:)forControlEvents:UIControlEventTouchUpInside];

            [self.viewaddSubview:button];

        }

    }

    /* 肚子 */

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

    label.backgroundColor = [UIColorlightGrayColor];

    label.alpha =0.1;

    [self.viewaddSubview:label];

    [label release];

    

    /* 尾巴 */

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

    tail.backgroundColor = [UIColorlightGrayColor];

    tail.alpha =0.1f;

    [self.viewaddSubview:tail];

    [tail release];

    

    /* 左脚 */

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

    footLife.backgroundColor = [UIColorlightGrayColor];

    footLife.alpha =0.1f;

    [self.viewaddSubview:footLife];

    [footLife release];

    

    /* 右脚 */

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

    footRight.backgroundColor = [UIColorlightGrayColor];

    footRight.alpha =0.1f;

    [self.viewaddSubview:footRight];

    [footRight release];

    

    /* */

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

    knockout.backgroundColor = [UIColorlightGrayColor];

    knockout.alpha =0.1f;

    [self.viewaddSubview:knockout];

    [knockout release];

    

#pragma mark ** 轻拍手势

    /* 1.创建对象 */

    UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tap:)];

    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tap1:)];

    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tap2:)];

    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tap3:)];

#pragma mark ** 长按手势

    UILongPressGestureRecognizer *lTap = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@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 = [NSMutableArrayarrayWithCapacity:0];

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

        /* 获取照片名字 */

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

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

        NSString *path = [[NSBundlemainBundle] pathForResource:imageNameofType:nil];

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

        UIImage *image = [UIImageimageWithContentsOfFile:path];

        arrayM[i] = image;

    }

    NSLog(@"%@", arrayM);

    /* 开始执行动画 */

    [self.tomsetAnimationImages:arrayM];

    [self.tomsetAnimationRepeatCount:1];

    /* 设置执行时间 */

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

    [self.tomstartAnimating];

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

    [self.tomperformSelector:@selector(setAnimationImages:)withObject:nilafterDelay:self.tom.animationDuration];

}

/* 尾巴 */

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

    NSLog(@"轻拍");

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

    NSMutableArray *arrayM = [NSMutableArrayarrayWithCapacity:0];

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

        /* 获取照片名字 */

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

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

        NSString *path = [[NSBundlemainBundle] pathForResource:imageNameofType:nil];

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

        UIImage *image = [UIImageimageWithContentsOfFile:path];

        arrayM[i] = image;

    }

    NSLog(@"%@", arrayM);

    /* 开始执行动画 */

    [self.tomsetAnimationImages:arrayM];

    [self.tomsetAnimationRepeatCount:1];

    /* 设置执行时间 */

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

    [self.tomstartAnimating];

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

    [self.tomperformSelector:@selector(setAnimationImages:)withObject:nilafterDelay:self.tom.animationDuration];

}

/* 左脚 */

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

    NSLog(@"轻拍");

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

    NSMutableArray *arrayM = [NSMutableArrayarrayWithCapacity:0];

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

        /* 获取照片名字 */

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

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

        NSString *path = [[NSBundlemainBundle] pathForResource:imageNameofType:nil];

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

        UIImage *image = [UIImageimageWithContentsOfFile:path];

        arrayM[i] = image;

    }

    NSLog(@"%@", arrayM);

    /* 开始执行动画 */

    [self.tomsetAnimationImages:arrayM];

    [self.tomsetAnimationRepeatCount:1];

    /* 设置执行时间 */

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

    [self.tomstartAnimating];

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

    [self.tomperformSelector:@selector(setAnimationImages:)withObject:nilafterDelay:self.tom.animationDuration];

}

/* 右脚 */

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

    NSLog(@"轻拍");

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

    NSMutableArray *arrayM = [NSMutableArrayarrayWithCapacity:0];

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

        /* 获取照片名字 */

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

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

        NSString *path = [[NSBundlemainBundle] pathForResource:imageNameofType:nil];

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

        UIImage *image = [UIImageimageWithContentsOfFile:path];

        arrayM[i] = image;

    }

    NSLog(@"%@", arrayM);

    /* 开始执行动画 */

    [self.tomsetAnimationImages:arrayM];

    [self.tomsetAnimationRepeatCount:1];

    /* 设置执行时间 */

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

    [self.tomstartAnimating];

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

    [self.tomperformSelector:@selector(setAnimationImages:)withObject:nilafterDelay:self.tom.animationDuration];

}


/* */


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

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

    if (lTap.state ==UIGestureRecognizerStateBegan) {

        NSMutableArray *arrayM = [NSMutableArrayarrayWithCapacity:0];

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

            /* 获取照片名字 */

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

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

            NSString *path = [[NSBundlemainBundle] pathForResource:imageNameofType:nil];

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

            UIImage *image = [UIImageimageWithContentsOfFile:path];

            arrayM[i] = image;

        }

        NSLog(@"%@", arrayM);

        /* 开始执行动画 */

        [self.tomsetAnimationImages:arrayM];

        [self.tomsetAnimationRepeatCount:1];

        /* 设置执行时间 */

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

        [self.tomstartAnimating];

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

        [self.tomperformSelector:@selector(setAnimationImages:)withObject:nilafterDelay:self.tom.animationDuration];

    }

    NSLog(@"%s", __func__);

}



/* button 点击事件 */

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

    NSLog(@"asdsadas");

    [selftomAnimationWithName:button.currentTitlecount:button.tag];

}


/*  触发事件  */

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

    NSMutableArray *arrayM = [NSMutableArrayarrayWithCapacity:0];

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

        /* 获取照片名字 */

        NSString *imageName = [NSStringstringWithFormat:@"%@_%02d.jpg",name,i];

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

        NSString *path = [[NSBundlemainBundle] pathForResource:imageNameofType:nil];

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

        UIImage *image = [UIImageimageWithContentsOfFile:path];

        arrayM[i] = image;

    }

    /* 开始执行动画 */

    [self.tomsetAnimationImages:arrayM];

    [self.tomsetAnimationRepeatCount:1];

    /* 设置执行时间 */

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

    [self.tomstartAnimating];

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

    [self.tomperformSelector:@selector(setAnimationImages:)withObject:nilafterDelay:self.tom.animationDuration];

}



0 0
原创粉丝点击