UI2_ButtonChess

时间:2023-03-08 23:50:18
UI2_ButtonChess
//
// AppDelegate.m
// UI2_ButtonChess
//
// Created by zhangxueming on 15/6/30.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "AppDelegate.h" @interface AppDelegate ()
{
UIButton *_lastBtn; //记录上次点击的btn
} @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self showButtonChess];
self.window.rootViewController = nil;
self.window.backgroundColor = [UIColor whiteColor];
return YES;
} - (void)showButtonChess
{
NSArray *titles = @[@"車",@"马",@"象",@"王",@"后",@"象",@"马",@"車"]; CGFloat size = self.window.frame.size.width/8; for (int i=0; i<8; i++) {
for (int j=0; j<8; j++) {
UIView *view =[[UIView alloc] initWithFrame:
CGRectMake(j*size, 100+i*size, size, size)];
if ((i+j)%2) {
view.backgroundColor = [UIColor yellowColor];
}
else
{
view.backgroundColor= [UIColor cyanColor];
}
[self.window addSubview:view];
}
}
for (int i=0; i<8; i++) {
for (int j=0; j<8; j++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(j*size, 100+i*size, size, size);
if (i==0||i==7) {
[btn setTitle:titles[j] forState:UIControlStateNormal];
}
if (i==1||i==6) {
[btn setTitle:@"兵" forState:UIControlStateNormal];
} if (i==0||i==1) {
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:30];
}
if (i==6||i==7) {
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:30];
}
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btn];
}
}
} - (void)btnClick:(UIButton *)btn
{
if (_lastBtn && ![btn.currentTitle length]) {
CGRect frame = _lastBtn.frame;
_lastBtn.frame = btn.frame;
btn.frame = frame;
_lastBtn = nil;
}
else if (!_lastBtn && btn.currentTitle.length)
{
_lastBtn = btn;
}
//[self.window bringSubviewToFront:btn];
}