码迷,mamicode.com
首页 > 其他好文 > 详细

UI2_ButtonChess

时间:2015-07-11 13:31:13      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:

//
//  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];
}

 

UI2_ButtonChess

标签:

原文地址:http://www.cnblogs.com/0515offer/p/4638417.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!