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

代码优化---主方法尽量简洁

时间:2015-10-09 00:32:41      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

eg.超级猜图 <下一题按钮操作优化>

总效果:

技术分享

 1 #pragma mark 下一题
 2 -(IBAction)nextQuestion
 3 {
 4     //1.当前答题的索引
 5     self.index++;
 6     //2.从数组中按照索引取出题目模型数据
 7     HMQuestion *question = self.questions[self.index];
 8     
 9     //3.设置基本信息
10     [self setupBasicInfo:question];
11         //4.设置答案按钮
12     //首先清除掉答题区所有按钮
13     [self creatAnswerButtons:question];
14     //5.设置选项按钮
15     [self createOptionButtons:question];
16 }
17 
18 
19 /**设置基本信息*/
20 -(void)setupBasicInfo:(HMQuestion *)question
21 {
22     self.noLabel.text = [NSString stringWithFormat:@"%d/%d", self.index + 1, self.questions.count];
23     
24     self.titleLable.text = question.title;
25     [self.iconButton setImage:[UIImage imageNamed:question.icon] forState:UIControlStateNormal];
26     //如果到达下一题,禁用下一天牛
27     self.nextQuestionButton.enabled = (self.index <self.questions.count-1);
28     
29 
30 
31 }
32 
33 /**创建答案区按钮*/
34 
35 -(void)creatAnswerButtons:(HMQuestion *)question
36 {
37     for (UIView *btn in self.answerView.subviews) { //UIButton *btn in self.answerView.subviews 所有的UI控件都继承自UIView
38         [btn removeFromSuperview];
39         
40     }
41     CGFloat answerW = self.answerView.bounds.size.width;
42     int lenght = question.answer.length;
43     CGFloat answerX = (answerW - kButtonWidth *lenght -kButtonMargin *(lenght -1))*0.5;
44     //创建所有答案按钮
45     for (int i = 0 ; i < lenght; i++) {
46         CGFloat x = answerX +i*( kButtonMargin + kButtonWidth );
47         UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, kButtonWidth, kButtonHeight)];
48         // btn.backgroundColor =[UIColor whiteColor];
49         [btn setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
50         [btn setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"]forState:UIControlStateHighlighted];
51         
52         [self.answerView addSubview:btn];
53     }
54     
55 
56 
57 
58 }
59 
60 /** 被选取按钮*/
61 -(void)createOptionButtons:(HMQuestion *)question
62 {
63     for (UIView *view in self.optionsView.subviews) {
64         [view  removeFromSuperview];
65     }
66     CGFloat optionW = self.optionsView.bounds.size.width;
67     CGFloat optionX = (optionW - kTotolCol * kButtonWidth -(kTotolCol - 1)*kButtonMargin)*0.5;
68     for (int i = 0 ; i  <question.options.count; i++) {
69         int row =i/kTotolCol;//
70         int col = i%kTotolCol;//
71         CGFloat x = optionX +col * (kButtonMargin +kButtonWidth);
72         CGFloat y = row * (kButtonMargin + kButtonHeight);
73         UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, kButtonWidth, kButtonHeight)];
74         
75         
76         //btn.backgroundColor =[UIColor whiteColor];
77         [btn setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal];
78         [btn setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted];
79         //设置备选答案
80         [btn setTitle:question.options[i] forState:UIControlStateNormal];
81         [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
82         
83         [self.optionsView addSubview:btn];
84         
85     }
86 
87 }

 

代码优化---主方法尽量简洁

标签:

原文地址:http://www.cnblogs.com/Simplelee/p/4862677.html

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