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);
}
// 实现:点击任何一颗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);
}
相关文章推荐
- UITableView 单元格自定义个别不能滑动删除
- UIButton选中状态下的点击
- UI更新操作
- UICollectionView如果在数据不够一屏时上下滚动
- 子线程中更新主线程中的UI
- HDU 1159.Common Subsequence【动态规划DP】【3月8】
- SpannableString与SpannableStringBuilder
- UICollectionView
- 错误 finished with non-zero exit value 1
- Handler、Looper、MessageQueen、Message的关系及个人理解
- Java图形化用户界面(GUI)笔记(一)介绍
- ZOJ 3333 Guess the Price
- Java中String、StringBuffer和StringBuilder的区别
- 【Xamarin挖墙脚系列:代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧(转)】
- hbuilder 导入git 项目
- SystemUI源码分析
- uitesting 问题汇总
- 1007. Maximum Subsequence Sum (25)
- easyui主界面生成分析-4
- IOS 多个UIImageView 加载高清大图时内存管理