创建九宫格:
for (int i = 0; i < 4; i++) { int a = 50 + i % 3 * 60 + i % 3 * 20; int b = 50 + i / 3 * 60 + i / 3 * 20; UIButton * bun = [UIButton buttonWithType:UIButtonTypeCustom]; bun.frame = CGRectMake(a, b, 60, 60); bun.backgroundColor = [UIColor redColor]; [self.view addSubview:bun]; }
添加一个取余和取整的方法:
swift版本:
for i in 0...3 { print("取余是:(i%3)") } for i in 0...3 { print("取整是:(i/3)") }
控制台打印如下: