zoukankan      html  css  js  c++  java
  • 汤姆猫实例代码(无配音)

     

    #import "ViewController.h"

     

    // 视图大小

    #define VIEW_W 60

    #define VIEW_H 60

     

    // 总个数

    #define kSIZE 6

    // 每行个数

    #define kNumber 2

    // 状态栏

    #define kStart (20+280)

    // y间距

    #define MarGinY 10

     

    // 屏幕

    #define SCREEN_W self.view.bounds.size.width

    #define SCREEN_H self.view.bounds.size.height

     

    @interface ViewController ()

     

    @property (nonatomic,strong) UIImageView * tomCatImageView;

    @property (nonatomic,strong) NSArray * buttonTitleArr;

    @property (nonatomic,strong) NSArray * arr;

     

    @property (nonatomic,strong) NSArray *picNumArr;

     

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        

        [super viewDidLoad];

        [self creatUI];

    }

     

    #pragma mark - 创建TomCat页面布局

    - (void)creatUI

    {

    //    指定一张图片为背景图片

        _tomCatImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];

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

        

        [self.view addSubview:_tomCatImageView];

        

    //    存储文件名,便于之后的操作

        _buttonTitleArr = @[@"cymbal",@"drink",

                            @"eat",@"fart",

                            @"pie",@"scratch"];

        

    //    存储对应图片文件的图片数量

        _picNumArr = @[@"13",@"81",

                       @"40",@"28",

                       @"24",@"56"];

    //    九宫图布局

        for (int i=0; i<kSIZE; i++)

        {

            CGFloat marginX = (SCREEN_W - (VIEW_W*kNumber)) / (kNumber + 1);

            CGFloat marginY = MarGinY;

            int row = i % kNumber;

            int low = i / kNumber;

     

            CGFloat x = marginX + (VIEW_W + marginX) * row;

            CGFloat y = kStart + marginY + (VIEW_H + marginY) * low;

            

            UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];

            if(row == 0)

            {

                button.frame = CGRectMake(x - 70, y, VIEW_W, VIEW_H);

            }else

                button.frame = CGRectMake(x + 70, y, VIEW_W, VIEW_H);

            

            [button setTitle:_buttonTitleArr[i] forState:UIControlStateNormal];

    //        清除buttontitle的颜色

            [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];

    //        设置按钮图片,因为图片为.png格式,省去后缀

            [button setBackgroundImage:[UIImage imageNamed:_buttonTitleArr[i]] forState:UIControlStateNormal];

            [self.view addSubview:button];

            //添加按钮事件

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

        }

     

        

        _arr = @[@"knockout",@"stomach",@"footRight",@"footLeft",@"angry"];

        for (int i = 0 ; i < [_arr count]; i++)

        {

            UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];

            

            switch (i)

            {

                //头部位置

                case 0:

                    btn.frame = CGRectMake(68, 95, 180, 180);

                    break;

                //猫的胃部位置

                case 1:

                    btn.frame = CGRectMake(110, 400, 100, 100);

                    break;

                //右脚

                case 2:

                    btn.frame = CGRectMake(112, 515, 35, 30);

                    break;

                //左脚

                case 3:

                    btn.frame = CGRectMake(167, 515, 35, 30);

                    break;

                //生气,接近尾巴的位置

                case 4:

                    btn.frame = CGRectMake(215, 450, 30, 60);

                    break;

                default:

                    break;

            }

            [btn setTitle:_arr[i] forState:UIControlStateNormal];

            //清除颜色

            [btn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];

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

            [self.view addSubview:btn];

        }

         

    }

     

    #pragma mark - 按钮的点击事件

    //  获取动画所需要的参数

    - (void)buttonClicked:(UIButton *)button

    {

        for (int i = 0 ; i < _picNumArr.count; i++)

        {

            if ([button.currentTitle isEqualToString:_buttonTitleArr[i]])

            {

    //            获得当前按钮对应的图片文件名字和对应的图片个数传到

                [self setAnimatingWithNum:_picNumArr[i] andImageName:_buttonTitleArr[i]];

            }

        }

    }

     

     

    - (void)btnClicked:(UIButton *)btn

    {

        

        NSArray * picNumArr = @[@"81",@"34",@"29",@"30",@"26"];

        for (int i = 0 ; i < [_arr count]; i++)

        {

            if ([btn.currentTitle isEqualToString:_arr[i]])

            {

                [self setAnimatingWithNum:picNumArr[i] andImageName:_arr[i]];

            }

        }

    }

     

     

    #pragma mark - 执行动画操作

    - (void)setAnimatingWithNum:(NSString *)picNum andImageName:(NSString * )imageName

    {

    //    TomCat正在进行动作时,不能继续点击其他动作

        if ([_tomCatImageView isAnimating])

        {

            return ;

        }

        

        NSMutableArray * arr = [NSMutableArray array];

        for (int i = 0 ; i < picNum.intValue; i++)

        {

            _tomCatImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_%02d.jpg",imageName,i]];

            [arr addObject:_tomCatImageView.image];

        }

        //动画数组

        _tomCatImageView.animationImages = arr;

        //动画的持续时间

        _tomCatImageView.animationDuration = arr.count*0.1;

        //动画只重复一次,当值为0表示一直进行,不会停止下来

        _tomCatImageView.animationRepeatCount = 1;

        [_tomCatImageView startAnimating];

    }

    @end

  • 相关阅读:
    ||和&&
    用jQuery编的一个分页小代码
    Intent携带额外的数据的方法
    Handler消息传递机制
    安卓中的消息提示
    使用AlertDialog创建对话框的大致步骤
    布局管理器
    Android中支持的常用距离单位
    开发自定义View
    Gridview中奇偶数行颜色设置
  • 原文地址:https://www.cnblogs.com/Mr-Lin/p/5097152.html
Copyright © 2011-2022 走看看