IOS第四天(5:创建备份区按钮和判断结果)

创建备份区按钮和判断结果

/** 创建备选区按钮 */
- (void)createOptionButtons:(HMQuestion *)question
{
    // 问题:每次调用下一题方法时,都会重新创建21个按钮
    // 解决:如果按钮已经存在,并且是21个,只需要更改按钮标题即可
    if (self.optionsView.subviews.count != question.options.count) {
        // 重新创建所有按钮
        for (UIView *view in self.optionsView.subviews) {
            [view removeFromSuperview];
        }
        
        CGFloat optionW = self.optionsView.bounds.size.width;
        CGFloat optionX = (optionW - kTotolCol * kButtonWidth - (kTotolCol - 1) * kButtonMargin) * 0.5;
        
        for (int i = 0; i < question.options.count; i++) {
            int row = i / kTotolCol;
            int col = i % kTotolCol;
            
            CGFloat x = optionX + col * (kButtonMargin + kButtonWidth);
            CGFloat y = row * (kButtonMargin + kButtonHeight);
            
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, kButtonWidth, kButtonHeight)];
            [btn setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted];
            
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            [self.optionsView addSubview:btn];
            
            // 添加按钮监听方法
            [btn addTarget:self action:@selector(optionClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        NSLog(@"创建候选按钮");
    }
    
    // 如果按钮已经存在,在点击下一题的时候,只需要设置标题即可
    int i = 0;
    for (UIButton *btn in self.optionsView.subviews) {
        // 设置备选答案
        [btn setTitle:question.options[i++] forState:UIControlStateNormal];
        
        // 回复所有按钮的隐藏状态
        btn.hidden = NO;
        
//        // 添加按钮监听方法
        // 提示,如果再次添加监听方法,意味着每次调用下一题的时候都会添加监听方法
//        [btn addTarget:self action:@selector(optionClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

#pragma mark - 候选按钮点击方法
/** 候选按钮点击 */
- (void)optionClick:(UIButton *)button
{
    // 1. 在答案区找到第一个文字为空的按钮
    UIButton *btn = [self firstAnswerButton];
    
    // 如果没有找到按钮,直接返回
    if (btn == nil) return;
    
    // 2. 将button的标题设置给答案区的按钮
    [btn setTitle:button.currentTitle forState:UIControlStateNormal];
    
    // 3. 将button隐藏
    button.hidden = YES;
    
    // 4. 判断结果
    [self judge];
}

/** 判断结果 */
- (void)judge
{
    // 如果四个按钮都有文字,才需要判断结果
    // 遍历所有答题区的按钮
    BOOL isFull = YES;
    NSMutableString *strM = [NSMutableString string];
    
    for (UIButton *btn in self.answerView.subviews) {
        if (btn.currentTitle.length == 0) {
            // 只要有一个按钮没有字
            isFull = NO;
            break;
        } else {
            // 有字,拼接临时字符串
            [strM appendString:btn.currentTitle];
        }
    }
    
    if (isFull) {
        NSLog(@"都有字");
        // 判断是否和答案一致
        // 根据self.index获得当前的question
        HMQuestion *question = self.questions[self.index];
        
        // 如果一致,进入下一题
        if ([strM isEqualToString:question.answer]) {
            NSLog(@"答对了");
            [self setAnswerButtonsColor:[UIColor blueColor]];
            
            // 等待0.5秒,进入下一题
            [self performSelector:@selector(nextQuestion) withObject:nil afterDelay:0.5];
        } else {
            // 如果不一致,修改按钮文字颜色,提示用户
            NSLog(@"答错了");
            [self setAnswerButtonsColor:[UIColor redColor]];
        }
    }
}

/** 修改答题区按钮的颜色 */
- (void)setAnswerButtonsColor:(UIColor *)color
{
    for (UIButton *btn in self.answerView.subviews) {
        [btn setTitleColor:color forState:UIControlStateNormal];
    }
}

// 在答案区找到第一个文字为空的按钮
- (UIButton *)firstAnswerButton
{
    // 取按钮的标题
    // 遍历答题区所有按钮
    for (UIButton *btn in self.answerView.subviews) {
        if (btn.currentTitle.length == 0) {
            return btn;
        }
    }
    return nil;
}

 

posted @ 2015-07-30 17:43  iso  阅读(172)  评论(0编辑  收藏  举报