iOS笔记UI--汤姆猫小项目(UIbutton使用)

/*

 项目思路

1.      先创建一个UIImageView放背景图片

2.      再创建一个UIImageView 放要执行动作的图片

3.      最顶层创建一个UIView放有图片button和透明button

4.      创建button方法,用switch通过tag值判断是哪个button,从而执行相应的动画效果 */

附上代码和素材以供练习:http://yunpan.cn/cHNjcsRsMF6PV  访问密码 89de
@implementation ViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cyanColor];
    
    // 背景图片的imgView
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:self.view.frame];
    [imageView1 setImage:[UIImage imageNamed:@"cat_cymbal_iPad0000.jpg"]];
    [self.view addSubview:imageView1];
    
    // 动作图片的imgView
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
    imageView.tag =100;
    [self.view addSubview:imageView];
    
    //放按钮的view
    UIView * btnView = [[UIView alloc]init];
    btnView.frame = self.view.frame;
    [self.view addSubview:btnView];

    // 按钮照片
    NSArray * btnPicArr = @[@"cynbals@2x.png",@"pawn@2x.png",
                            @"larry@2x.png",@"milk@2x.png",
                            @"pie@2x.png"];
    
    // 有图按钮的布局   tag == 1
    UIButton * picBtn;
    for (NSUInteger x  = 1; x < 6; x++) {
        
        picBtn= [self myBtnRect:CGRectMake(60*x-20, 30, 50, 50) BtnBackgroundColor:[UIColor clearColor] BtnImg:[UIImage imageNamed:btnPicArr[x-1]] Action:@selector(click:) addView:btnView];
        picBtn.tag = x;   
    }
    
    // 无图按钮布局   // tag == 50;
    UIButton *notPicBtn;
    CGRect rect ;
    for (NSUInteger i = 50; i < 56; i++) {
        switch (i) {
            case 50:
                // 头部
                rect = CGRectMake(self.view.frame.size.width/2-70, 120, 130, 50);
                break;
            case 51:
                // 右脚
                rect = CGRectMake(130, self.view.frame.size.height/2+260, 50, 50);
                break;
            case 52:
                // 左脚
                rect = CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2+260, 50, 50);
                break;
            case 53:
                // 右手
                rect = CGRectMake(90, self.view.frame.size.height/2+50, 50, 50);
                break;
            case 54:
                // 尾巴
                rect = CGRectMake(250, self.view.frame.size.height-160, 50, 100);
                break;
            case 55:
                // 肚子
                rect = CGRectMake(self.view.frame.size.width/2-50, self.view.frame.size.height/2+100, 100, 100);
                break;       
        }
        notPicBtn = [self myBtnRect:rect BtnBackgroundColor:[UIColor clearColor] BtnImg:nil Action:@selector(click:) addView:btnView];
        notPicBtn.tag = i;   
    }   
}


#pragma mark  按钮点击方法
-(void)click:(UIButton*)btn{
    UIImageView *myImageView =(UIImageView*)[self.view viewWithTag:100];
    NSMutableArray *mutCymbal = [NSMutableArray array];
    // 在这里判断是哪个被点击
    switch (btn.tag) {
        case 1:
            // 敲锣
            for (NSUInteger i = 0; i<13; i++) {
                UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"cat_cymbal_iPad%04ld.jpg",i]];
                [mutCymbal addObject:img];
                [self myAnimatingNSMutableArray:mutCymbal Btn:btn Timer:13 WindowView:myImageView];
            }
            break;
        case 2:
            //  抓屏幕
            for (NSUInteger i = 0; i<55; i++) {
                UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"Scratch-iPad%04ld.jpg",i]];
                [mutCymbal addObject:img];
                [self myAnimatingNSMutableArray:mutCymbal Btn:btn Timer:55 WindowView:myImageView];
            }
            break;
            
        case 3:
            // 吃鸟
            for (NSUInteger i = 0; i<35; i++) {
                UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"cat_eat_iPad%04ld.jpg",i]];
                [mutCymbal addObject:img];
                [self myAnimatingNSMutableArray:mutCymbal Btn:btn Timer:35 WindowView:myImageView];
            }
            break;
            
        case 4:
            // 喝牛奶
            for (NSUInteger i = 0; i<81; i++) {
                UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"cat_drink%04ld.jpg",i]];
                [mutCymbal addObject:img];
                [self myAnimatingNSMutableArray:mutCymbal Btn:btn Timer:81 WindowView:myImageView] ;
            }
            break;
            
        case 5:
            // 扔东西
            for (NSUInteger i = 0; i<23; i++) {
                UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"pie-iPad%04ld.jpg",i]];
                [mutCymbal addObject:img];
                [self myAnimatingNSMutableArray:mutCymbal Btn:btn Timer:23 WindowView:myImageView];
            }
            break;
            
            // 以下为透明按钮
            
        case 50:
            // 头被敲打
            for (NSUInteger i = 0; i<80; i++) {
                UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"cat_knockout%04ld.jpg",i]];
                [mutCymbal addObject:img];
                [self myAnimatingNSMutableArray:mutCymbal Btn:btn Timer:80 WindowView:myImageView];
            }
            break;
            
        case 51:
            // 右脚
            for (NSUInteger i = 0; i<29; i++) {
                UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"cat_foot_right%04ld.jpg",i]];
                [mutCymbal addObject:img];
                [self myAnimatingNSMutableArray:mutCymbal Btn:btn Timer:29 WindowView:myImageView];
            }
            break;
            
        case 52:
            // 左脚
            for (NSUInteger i = 0; i<29; i++) {
                UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"cat_foot_left%04ld.jpg",i]];
                [mutCymbal addObject:img];
                [self myAnimatingNSMutableArray:mutCymbal Btn:btn Timer:29 WindowView:myImageView];
            }
            break;
        case 53:
            // 右手
            for (NSUInteger i = 0; i<27; i++) {
                UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"iPad%04ld.jpg",i]];
                [mutCymbal addObject:img];
                [self myAnimatingNSMutableArray:mutCymbal Btn:btn Timer:27 WindowView:myImageView];
            }
            break;
        case 54:
            // 尾巴
            for (NSUInteger i = 0; i<25; i++) {
                UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"cat_angry%04ld.jpg",i]];
                [mutCymbal addObject:img];
                [self myAnimatingNSMutableArray:mutCymbal Btn:btn Timer:25 WindowView:myImageView];
            }
            break;
        case 55:
            // 肚子
            for (NSUInteger i = 0; i<33; i++) {
                UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"cat_stomach%04ld.jpg",i]];
                [mutCymbal addObject:img];
                [self myAnimatingNSMutableArray:mutCymbal Btn:btn Timer:33 WindowView:myImageView];
            }
            break;       
    }
}

#pragma mark - btn创建的方法
-(UIButton *)myBtnRect:(CGRect)cgrect BtnBackgroundColor:(UIColor*)backgroundColor BtnImg:(UIImage *)btnimg Action:(SEL)click addView:(UIView *)view{
    
    UIButton *myButton =  [UIButton buttonWithType:UIButtonTypeCustom];
    myButton.frame = cgrect;
    myButton.backgroundColor = backgroundColor;
    [view addSubview:myButton];
    [myButton setBackgroundImage:btnimg forState:UIControlStateNormal];
    [myButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    return myButton;
}

#pragma mark  btnClick创建的方法
-(void)myAnimatingNSMutableArray:(NSMutableArray *)mutCymbal Btn:(UIButton *)btn Timer:(NSUInteger)timer WindowView:(UIView *)windowView{
    // 点击产生动作
    // UIImageView 加入第一张图片
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:self.view.frame];
    imgView.image = mutCymbal[0];
    [self.view addSubview:imgView];
    imgView.animationImages = mutCymbal;           // 动画内容
    imgView.animationDuration = (timer*0.1);       // 动画时间
    imgView.animationRepeatCount = YES;
    [imgView startAnimating];                     // 开始动画
    [self.view insertSubview:imgView belowSubview:windowView];
}

@end //  ViewController                  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值