1,首先创建4*4的16个按钮,先把所有按钮设置为白色,再在每层产生一个0-3的随机数,将对应的按钮设置为黑色显示出来,如图:
代码如下:
for (int j = 0; j < 4; j++) {
int arc = arc4random() % 4;
for (int i = 0; i < 4; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(80 * i, 120 * j, 80, 120);
button.tag = 101 + i + j * 10;
button.layer.borderWidth = 0.5;
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];