複数選択の質問に対する正解であるかどうかに応じて、(選択されたときに) ボタンに色を付けるボタン サブクラスを作成しようとしています。
私はこれを次のようにやっています: 私の 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 のセッター メソッド:
- (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 の私の draw メソッドでは:
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;
}
touch のオーバーライド:
-(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 が呼び出されますが、ボタンの色は設定されておらず、白くすばやく点滅してから、選択されていない色に戻ります.. .