Another simple solution for silver challenge


#1

I worked on this challenge for quite a while and found another way to complete it.
I also use 2 labels for question and answers as pmac72 did. And animate them using keyframe animation. After the animation is done I update the label’s text and change their pointers to get ready for the next move.

@property (nonatomic, weak) UILabel *questionLabel; // This pointer always points to the label showing the current question @property (nonatomic, weak) UILabel *nQuestionLabel; // This pointer always points to the question label waiting to be shown @property (nonatomic, weak) UILabel *answerLabel; // This pointer always points to the label showing the current answer @property (nonatomic, weak) UILabel *nAnswerLabel; // This pointer always points to the answer label waiting to be shown

Create 4 labels programatically and get them prepared:

[code]- (void)viewDidLoad
{
[super viewDidLoad];

self.answerButton.enabled = NO;

// Configure the first question label
UILabel *questionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 129, 280, 50)];
questionLabel.text = @"Press Show Question\nto get a question";
questionLabel.textAlignment = NSTextAlignmentCenter;
questionLabel.numberOfLines = 2;
[self.view addSubview:questionLabel];
self.questionLabel = questionLabel;

// Configure the second question label waiting to be shown;
UILabel *nQuestionLabel = [[UILabel alloc] initWithFrame:CGRectMake(-140, 129, 280, 50)];
NSString *question = self.questions[self.currentQuestionIndex];
nQuestionLabel.text = question;
nQuestionLabel.textAlignment = NSTextAlignmentCenter;
nQuestionLabel.numberOfLines = 2;
nQuestionLabel.alpha = 0;
[self.view addSubview:nQuestionLabel];
self.nQuestionLabel = nQuestionLabel;

// Configure the first answer label
UILabel *answerLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 330, 280, 50)];
answerLabel.text = @"Press Show Answer\nto reveal answer";
answerLabel.textAlignment = NSTextAlignmentCenter;
answerLabel.numberOfLines = 2;
[self.view addSubview:answerLabel];
self.answerLabel = answerLabel;

// Configure the secon answer label
UILabel *nAnswerLabel = [[UILabel alloc] initWithFrame:CGRectMake(-140, 330, 280, 50)];
NSString *answer = self.answers[self.currentQuestionIndex];
nAnswerLabel.text = answer;
nAnswerLabel.textAlignment = NSTextAlignmentCenter;
nAnswerLabel.numberOfLines = 2;
nAnswerLabel.alpha = 0;
[self.view addSubview:nAnswerLabel];
self.nAnswerLabel = nAnswerLabel;

}[/code]

The action called when user presses Show Question button. the answer for the question will be prepared in this action.

[code]- (IBAction)showQuestion:(id)sender
{
self.questionButton.enabled = NO;
self.answerButton.enabled = NO;
[UIView animateKeyframesWithDuration:1
delay:0.0
options:0
animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0
relativeDuration:0.5
animations:^{
self.questionLabel.center = CGPointMake(320, self.questionLabel.center.y);
self.questionLabel.alpha = 0;
}];

                              [UIView addKeyframeWithRelativeStartTime:0.5
                                                      relativeDuration:0.5
                                                            animations:^{
                                                                self.nQuestionLabel.center = CGPointMake(160, self.nQuestionLabel.center.y);
                                                                self.nQuestionLabel.alpha = 1;
                                                            }];
                          }
                          completion:^(BOOL finished){
                              self.questionLabel.center = CGPointMake(0, self.questionLabel.center.y);
                              
                              // Step to the next question
                              self.currentQuestionIndex++;
                              
                              // Am I passed the last question?
                              if (self.currentQuestionIndex == [self.questions count]) {
                                  // Go back to the first quesion
                                  self.currentQuestionIndex = 0;
                              }
                              
                              // Get the string at that index in the questions array
                              NSString *question = self.questions[self.currentQuestionIndex];
                              
                              // Display the string in the question label
                              self.questionLabel.text = question;
                              
                              // Exchange pointers
                              UILabel *temp = self.questionLabel;
                              self.questionLabel = self.nQuestionLabel;
                              self.nQuestionLabel = temp;
                              temp = nil;
                              
                              self.questionButton.enabled = YES;
                              self.answerButton.enabled = YES;
                          }];

self.answerLabel.text = @"Think about the answer.";
NSString *answer = self.answers[self.currentQuestionIndex];
self.nAnswerLabel.text = answer;

}[/code]

The action called when user presses the Show Answer button. This action just aims at animation.

[code]- (IBAction)showAnswer:(id)sender
{
self.answerButton.enabled = NO;
[UIView animateKeyframesWithDuration:1
delay:0.0
options:0
animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0
relativeDuration:0.5
animations:^{
self.answerLabel.center = CGPointMake(320, self.answerLabel.center.y);
self.answerLabel.alpha = 0;
}];

                              [UIView addKeyframeWithRelativeStartTime:0.5
                                                      relativeDuration:0.5
                                                            animations:^{
                                                                self.nAnswerLabel.center = CGPointMake(160, self.nAnswerLabel.center.y);
                                                                self.nAnswerLabel.alpha = 1;
                                                            }];
                          }
                          completion:^(BOOL finished){
                              self.answerLabel.center = CGPointMake(0, self.answerLabel.center.y);
                              
                              // Exchange pointers
                              UILabel *temp = self.answerLabel;
                              self.answerLabel = self.nAnswerLabel;
                              self.nAnswerLabel = temp;
                              temp = nil;
                              
                          }];

}[/code]