ios学习记录 day28 UI 6 关灯

MainViewController.m

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     // Do any additional setup after loading the view.
 5     TouchView * backView = [[TouchView alloc] initWithFrame:CGRectMake(0, 20, 320, 450)];
 6     [backView setBackgroundColor:[UIColor lightGrayColor]];
 7     [self.view addSubview:backView];
 8     [backView release];
 9       
10 }

TouchView.m

 1 - (void)dealloc
 2 {
 3     [super dealloc];
 4 }
 5 - (id)initWithFrame:(CGRect)frame
 6 {
 7     self = [super initWithFrame:frame];
 8     if (self) {
 9         // Initialization code
10         [self createbutton];
11     }
12     return self;
13 }
14 
15 
16 - (void)createbutton
17 {
18     
19     for (int i = 0; i < 25; i++) {
20         //循环 5行5列 15 70 起始button和backview的距离
21         CGFloat x = i % 5 * 60 + 15;
22         CGFloat y = i / 5 * 60 + 70;
23         //设置button的坐标大小
24         UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
25         [button setFrame:CGRectMake(x, y, 50, 50)];
26         //button的tag值1~25 不能为0! 通过tag值判断按钮的边界
27         button.tag = i + 1;
28         //为button添加点击事件
29         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
30         
31         //button的selected是个BOOL类型的值
32         //随机产生0和1 1的时候令selected为真 0时为假 这样程序开始的时候按钮的背景图片就是随机的
33         if (arc4random()%2) {
34             [button setSelected:true];
35         }else{
36             [button setSelected:false];
37         }
38         //选中按钮时背景图片on.png 正常是off.png
39         [button setBackgroundImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateSelected];
40         [button setBackgroundImage:[UIImage imageNamed:@"off.png"] forState:UIControlStateNormal];
41         
42         [self addSubview:button];
43     }
44 }
45 
46 - (void)buttonAction:(UIButton *)sender
47 {
48     //点击按钮时 先让按钮自身调用变换背景的方法
49     [self changeColor:sender];
50     //if判断游戏按钮的四个边
51     //除了最左边 其它按钮点击的时候其左边的按钮调用变换背景图片方法
52     if (!(sender.tag % 5 == 1)) {
53         UIButton *button1 = (UIButton *)[self viewWithTag:sender.tag - 1];
54         [self changeColor:button1];
55     }
56     //除了最上边 其它按钮点击的时候其上边的按钮调用变换背景图片方法
57     if (!((sender.tag - 1) / 5 == 0)) {
58         UIButton *button = (UIButton *)[self viewWithTag:sender.tag - 5];
59         [self changeColor:button];
60     }
61     //除了最右边 其它按钮点击的时候其右边的按钮调用变换背景图片方法
62     if (!(sender.tag % 5 == 0)) {
63         UIButton *button = (UIButton *)[self viewWithTag:sender.tag + 1];
64         [self changeColor:button];
65     }
66     //除了最下边 其它按钮点击的时候其下边的按钮调用变换背景图片方法
67     if (!(sender.tag > 20)) {
68         UIButton *button = (UIButton *)[self viewWithTag:sender.tag + 5];
69         [self changeColor:button];
70     }
71     
72 }
73 
74 //变换背景
75 - (void)changeColor:(UIButton *)sender
76 {
77     //改变按钮selected的BOOL就相当于改变了按钮的背景图片
78     sender.selected = !sender.selected;
79     
80 //    //按钮是色块的时候 点击按钮时直接改色就行
81 //    if (sender.backgroundColor == [UIColor greenColor]) {
82 //        sender.backgroundColor = [UIColor whiteColor];
83 //    }else{
84 //        sender.backgroundColor = [UIColor greenColor];
85 //    }
86 }

 

posted @ 2014-04-09 21:05  lxl奋小斗  阅读(176)  评论(0编辑  收藏  举报