您的位置:首页 > 产品设计 > UI/UE

UIButton 关灯小实验

2016-03-08 20:35 405 查看
// 写在继承于UIViewController的子类中:创建单视图默认有ViewController类

// 实现:点击任何一颗UIButton,它四周的以及它自身都被变成红色,再点击就会变成原来的灰色


- (void)viewDidLoad

{

[super viewDidLoad];

NSInteger numi = 15, i = 0, j = 0;

for (; i<13; i++)

{

NSInteger numj = 30; j = 0;

for (; j<23; j++)

{

UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(numi+i*30, numj+j*30, 20, 20)];

button.layer.cornerRadius = 10;

button.backgroundColor = [UIColor grayColor];

[self.view addSubview:button];

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

// 给button.tag编号

button.tag = 101 + i + 13 * j;

}

}

}

- (void)buttonclik:(UIButton *)button;

{

NSInteger arr[5] = {button.tag, button.tag+13, button.tag-13, button.tag-1, button.tag+1};

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

{

if (button.tag==101+13*i)

{

arr[3] = 100;

}

if (button.tag==113+13*i)

{

arr[4] = 100;

}

}

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

{

UIButton *temB = [self.view viewWithTag:arr[i]];

if (temB.backgroundColor == [UIColor grayColor])

{

temB.backgroundColor = [UIColor redColor];

}else

{

temB.backgroundColor = [UIColor grayColor];

}

}

NSLog(@"%lu", button.tag);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: