UI控件闪灯

做出点一个控件然后他和他上下左右的4个控件一起变色。

 

#import "ViewController.h"
@interface ViewController ()


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //每个方块的高度和宽度
   CGFloat width=50    ;
    CGFloat height=50;
    //列数
    int lie=5;
    //一行之中的方块间隙
    CGFloat Xjianxi=(self.view.frame.size.width-width*lie)/(lie +1);
    //一列之中的方块间隙
    CGFloat Yjianxi=30;
    self.number=1;
    //循环,
    for (int i=0; i<45; i++) {
        int hangshu=i/lie;
        int lieshu=i%lie;
        self.butn=[[UIButton alloc]initWithFrame:CGRectMake(Xjianxi+lieshu*(Xjianxi+width), Yjianxi+hangshu*(height+Yjianxi), width, height)];
        self.butn.backgroundColor=[UIColor blueColor];
        self.view.tag=100;
        
        
        
        [self.butn addTarget:self action:@selector(Discoloration:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:self.butn];
        self.butn.tag=self.number++;
        

        
    }

    
        }



-(void)change
{
    if (self.backgroundColor==[UIColor redColor]) {
        self.backgroundColor=[UIColor blueColor];
    }
    else{
        self.backgroundColor=[UIColor redColor];
    }
}



-(void)Discoloration:(UIButton*)btn
{
//    [[self.view viewWithTag:btn.tag] change];
//    [[self.view viewWithTag:btn.tag-1] change];
//    [[self.view viewWithTag:btn.tag+1] change];
//    [[self.view viewWithTag:btn.tag+5] change];
//    [[self.view viewWithTag:btn.tag-5]change];
    self.butn=btn;
    
    if (self.butn.tag==1) {
        [[self.view viewWithTag:btn.tag ] change];
        [[self.view viewWithTag:btn.tag+1] change];
        [[self.view viewWithTag:btn.tag+5] change];
    }
        else if(self.butn.tag==5)
        {
            [[self.view viewWithTag:btn.tag] change];
            [[self.view viewWithTag:btn.tag-1] change];
            [[self.view viewWithTag:btn.tag+5] change];
        }
        else if(self.butn.tag==41)
        {
            [[self.view viewWithTag:btn.tag] change];
            [[self.view viewWithTag:btn.tag-5] change];
            [[self.view viewWithTag:btn.tag+1] change];
        }
        else if(self.butn.tag==45)
        {
            [[self.view viewWithTag:btn.tag] change];
            [[self.view viewWithTag:btn.tag-1] change];
            [[self.view viewWithTag:btn.tag-5] change];
        }
        else if(self.butn.tag%5==1)
        {
            [[self.view viewWithTag:btn.tag] change];
            [[self.view viewWithTag:btn.tag-5] change];
            [[self.view viewWithTag:btn.tag+1] change];
            [[self.view viewWithTag:btn.tag+5] change];
        }
        else if(self.butn.tag%5==0)
        {
            [[self.view viewWithTag:btn.tag] change];
            [[self.view viewWithTag:btn.tag-5] change];
            [[self.view viewWithTag:btn.tag-1] change];
            [[self.view viewWithTag:btn.tag+5] change];
        }
        else if(self.butn.tag%5==1)
        {
            [[self.view viewWithTag:btn.tag] change];
            [[self.view viewWithTag:btn.tag-5] change];
            [[self.view viewWithTag:btn.tag+1] change];
            [[self.view viewWithTag:btn.tag-5] change];
        }
        else
        {
            [[self.view viewWithTag:btn.tag] change];
            [[self.view viewWithTag:btn.tag-1] change];
            [[self.view viewWithTag:btn.tag+1] change];
            [[self.view viewWithTag:btn.tag-5] change];
            [[self.view viewWithTag:btn.tag+5] change];
        }
    }
    



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

转载于:https://www.cnblogs.com/fume/p/5256083.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值