iOS -UI湯姆貓實現(UIImageView)自己寫的湯姆貓,所學知識有限,代碼還有待改進 ==

@interface ViewController ()

{

    UIImageView *tom;

    UIImageView *tommy;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    [self background];

    

    tommy = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

    

    [self.view addSubview:tommy];

    

    [self button];

}


/**

 *  背景圖

 */

- (void)background

{

    tom =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(self.view.frame), CGRectGetMaxY(self.view.frame))];

    NSLog(@"%f  %f",self.view.bounds.size.width,self.view.bounds.size.height);

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

    [self.view addSubview:tom];

}


/**

 *  一系列按鈕

 */

- (void) button

{

    //    按鈕

    NSArray *anniu = @[@"cymbal.png",@"fart.png",@"eat.png"];

    NSArray *rightAnniu = @[@"drink",@"scratch",@"pie"];

    

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

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.frame = CGRectMake(2, 300+(60+10) * i, 60, 60);

        btn.tag = 200 + i;

        btn.backgroundColor = [UIColor clearColor];

        [btn setBackgroundImage:[UIImage imageNamed:anniu[i]] forState:UIControlStateNormal];

        [btn addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:btn];

    }

    

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

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.frame = CGRectMake(320-60-2, 300+(60+10) * i, 60, 60);

        btn.tag = 30 + i;

        [btn setBackgroundImage:[UIImage imageNamed:rightAnniu[i]]forState:UIControlStateNormal];

        [btn addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];


        [self.view addSubview:btn];

    }

    

    

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

       UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        button.frame = CGRectMake(45+(59+1)*i, 537-27,50, 35);


        button.tag = 100 + i;

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

        [self.view addSubview:button];

    }

    //    尾巴

    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];

    button3.frame = CGRectMake(215, 430, 40, 100);

    button3.tag = 20;

    [button3 addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];

//        button3.backgroundColor = [UIColor blackColor];

    [self.view addSubview:button3];

    //    腹部

    UIButton *button1 =[UIButton buttonWithType:UIButtonTypeCustom];

    button1.frame = CGRectMake(100, 300, 120, 200);

    button1.tag = 21;

    [button1 addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];

    

//            button1.backgroundColor = [UIColor blackColor];

    [self.view addSubview:button1];

    //    頭部

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];

    button2.frame = CGRectMake(80, 90, 160, 180);

    button2.tag = 22;

    [button2 addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];

//        button2.backgroundColor = [UIColor blackColor];

    [self.view addSubview:button2];



    

}


/**

 *

 *

 *  @param count 圖片的張數

 *  @param name  動作的名稱

 */

- (void)animationWithCount:(int)count andname:(NSString *)name

{

    if (tommy.isAnimating ) {

        return;

    }

    NSMutableArray *images = [NSMutableArray array];

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

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

        NSBundle *myBoundle = [NSBundle mainBundle];

        NSString *path = [myBoundle pathForResource:fileName ofType:nil];


        NSString *path1 = [[NSBundle mainBundle] pathForResource:@"TomCat" ofType:@"plist"];

        NSArray *list = [NSArray arrayWithContentsOfFile:path1];

        NSLog(@"%@",list);

               

//        UIImage *image1 = [UIImage imageWithContentsOfFile:path1];

        

        

        

        

        

        

        UIImage *image = [[UIImage alloc]initWithContentsOfFile:path];

        

        [images addObject:image];

        

    }

    

     tommy.animationImages = images;

    

    tommy.animationRepeatCount = 1;

    

    tommy.animationDuration = images.count*0.1;

    

     [tommy startAnimating];

    

    CGFloat delay = tommy.animationDuration + 0.5;

    

    [tommy performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];


    

}


/**

 *  點擊的時候

 *

 *  @param button 告訴系統是哪一個按鈕

 */

- (void) touchButton:(UIButton *)button

{

    switch (button.tag) {

        case 101:

           [self animationWithCount:29 andname:@"foot_right"];

            break;

        case 102:

           [self animationWithCount:29 andname:@"foot_left"];

            break;

        case 20:

          [self animationWithCount:25 andname:@"angry"];

            break;


        case 21:

           [self animationWithCount:33 andname:@"stomach"];

            break;


        case 22:

          [self animationWithCount:80 andname:@"knockout"];

            break;


        case 200:

            [self animationWithCount:12 andname:@"cymbal"];

            break;


        case 201:

            [self animationWithCount:27 andname:@"fart"];

            break;


        case 202:

            [self animationWithCount:39 andname:@"eat"];

            break;


        case 30:

            [self animationWithCount:80 andname:@"drink"];

            break;


        case 31:

            [self animationWithCount:55 andname:@"scratch"];

            break;


        case 32:

            [self animationWithCount:23 andname:@"pie"];

            break;

            


        default:

            break;

    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章