0

我正在尝试创建一个按钮子类,它根据它是否是多项选择问题的正确答案来为按钮着色(当它被选中时)。

我这样做是这样的:在我的 UIViewController 中:

- (IBAction)answerQuestion:(id)sender {
    QGPrettyButton *answerButton;
    if ([sender isKindOfClass:[UIButton class]]) {
        answerButton = (QGPrettyButton *)sender;
        answerButton.isCorrectAnswer = [self.quizGame checkAnswer:self.currentQuestion answer:answerButton.titleLabel.text];
        [self performSelector:@selector(removeQuestion) withObject:nil afterDelay:TIME_TO_SHOW_CORRECT_ANSWER];
    }
}

在 isCorrectAnswer 的 setter 方法中:

- (void)setIsCorrectAnswer:(BOOL)isCorrectAnswer
{
    self.selected = YES;
    [self setNeedsDisplay];
    [self performSelector:@selector(returnToUnselected) withObject:nil afterDelay:TIME_TO_SHOW_CORRECT_ANSWER];
    _isCorrectAnswer = isCorrectAnswer;
}

- (void)returnToUnselected
{
    self.selected = NO;
    [self setNeedsDisplay];
}

在我的 QGPrettyButton 绘制方法中:

CGFloat hue = UNSELECTED_HUE;
CGFloat saturation = UNSELECTED_SATURATION;

if (self.state == UIControlStateHighlighted) {
    hue = HOVERED_HUE;
    saturation = HOVERED_SATURATION;
}
if (self.state == UIControlStateSelected) {
    hue = self.isCorrectAnswer ? CORRECT_HUE : INCORRECT_HUE;
}

压倒一切的触摸:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self setNeedsDisplay];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    [self setNeedsDisplay];

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    [self setNeedsDisplay];
    [self performSelector:@selector(hesitateUpdate) withObject:nil afterDelay:0.1];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    [self setNeedsDisplay];
    [self performSelector:@selector(hesitateUpdate) withObject:nil afterDelay:0.1];
}

如果您将鼠标按下按钮并再次释放,这一切正常,但如果您真的快速点击它,则会调用 IBAction,但未设置按钮的颜色,它只是快速闪烁白色然后回到未选择的颜色.. .

4

2 回答 2

0

您可能会尝试[self setNeedsDisplay]从您的touchesEnded:withEvent:andtouchesCancelled:withEvent:方法中移出并进入您的hesitateUpdate方法。

于 2013-03-09T15:52:01.327 回答
0

你可以试试highlightedUIView 的属性。

如果您希望它突出显示,请执行以下操作:

self.highlighted = YES;

反之亦然。

于 2014-06-12T18:04:32.463 回答