標籤:style blog class code java tar
一,
首先為window建立根視圖控制器
在AppDelegate.h檔案中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//為window建立根視圖控制器 RootViewController *rootViewController = [[RootViewController alloc]init]; self.window.rootViewController = rootViewController;
在RootViewController.m檔案中
- (void)viewDidLoad
{
int count = 0;//定義count計數 為tag標記存值
UIImage *one = [UIImage imageNamed:@"h.png"];//設定一個圖片
for (int i=0; i<7; i++) {
for (int j=0; j<5; j++) {
count++;
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(60*j, 30+i*60, 48, 48);
button.tag = count;
[button setImage:one forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(touch:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
count+=5;//控制tag標記,實現下一行的tag標記
}
[superviewDidLoad];
// Do any additional setup after loading the view.
}
-(void)touch:(id)sender
{
UIButton *button = (UIButton*)sender;
for (int i=0; i<=65; i++) {
UIButton *current = (UIButton*)[self.view viewWithTag:i];//設定當前按鈕的點擊事件
//下面是控制tag左右,上下
if ((i==button.tag-1)||(i==button.tag+1)||(i==button.tag-10)||(i==button.tag+10)) {
if (current.currentImage ==[UIImage imageNamed:@"h.png"]) {
[current setImage:[UIImageimageNamed:@"g.png"] forState:UIControlStateNormal];
}
else
{
[current setImage:[UIImageimageNamed:@"h.png"] forState:UIControlStateNormal];
}
}
}
}
最後的結果是這樣的: