首页 > 代码库 > 变形方块

变形方块

//// ViewController.m// 变形方块//// Created by 卿鹏 on 14-11-26.// Copyright (c) 2014年 qingpeng. All rights reserved.//#import "ViewController.h"@interface ViewController (){ NSMutableArray *array; UIView *v;}@end@implementation ViewControllerint m1[16][4][4] ={ {{0,0,0,0},{0,1,1,0},{0,1,1,0},{0,0,0,0}}, {{0,0,0,0},{1,1,1,1},{0,0,0,0},{0,0,0,0}}, {{0,0,0,0},{1,1,0,0},{0,1,1,0},{0,0,0,0}}, {{0,0,0,0},{0,0,1,1},{0,1,1,0},{0,0,0,0}}, {{0,0,0,0},{0,1,0,0},{1,1,1,0},{0,0,0,0}}, {{0,0,0,0},{0,1,1,1},{0,0,1,0},{0,0,0,0}}, {{1,0,0,0},{1,1,0,0},{1,0,0,0},{0,0,0,0}}, {{0,0,0,1},{0,0,1,1},{0,0,0,1},{0,0,0,0}}, {{1,0,0,0},{1,0,0,0},{1,0,0,0},{1,1,0,0}}, {{0,0,0,1},{0,0,0,1},{0,0,0,1},{0,0,1,1}}, {{1,1,0,0},{1,0,0,0},{1,0,0,0},{1,0,0,0}}, {{0,0,1,1},{0,0,0,1},{0,0,0,1},{0,0,0,1}}, {{0,1,0,0},{1,1,0,0},{1,0,0,0},{0,0,0,0}}, {{0,0,1,0},{0,0,1,1},{0,0,0,1},{0,0,0,0}}, {{0,0,0,0},{1,1,1,1},{0,0,0,0},{0,0,0,0}}, {{0,0,0,0},{0,1,1,0},{0,1,1,0},{0,0,0,0}}};int n[4][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. array = [[NSMutableArray alloc] init]; for(int i=0;i<16; i++) { int row = i % 4; int column = i / 4 ; v = [[UIView alloc] initWithFrame:CGRectMake(row*42+100, column*42+150, 40, 40)]; [array addObject:v];//添加一个OC中的View对象 [self.view addSubview:v]; } for(int x=0;x<4;x++)//初始化界面 { for(int y=0; y<4; y++) { UIView *k = array[x*4+y];//从数组中取出UIview if (n[x][y]) { k.backgroundColor = [UIColor blueColor]; } else { k.backgroundColor = [UIColor grayColor]; } } }}- (IBAction)didButton:(UIButton *)sender{ static int n=0; if (n==15) { n = 0; } else { n ++; } for(int x=0;x<4;x++) { for(int y=0; y<4; y++) { UIView *k = array[x*4+y];//从数组中取出UIview if (m1[n][x][y]) { k.backgroundColor = [UIColor blueColor]; } else { k.backgroundColor = [UIColor grayColor]; } } }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end

变形方块