My Silver Challenge Solution


#1

I tried to make this solution without adding any additional properties

In BNRQuizViewController.m:


//
//  BNRQuizViewController.m
//  Quiz
//
//  Created by Rob on 2014-07-20.
//  Copyright (c) 2014 PilSoft. All rights reserved.
//

#import "BNRQuizViewController.h"

@interface BNRQuizViewController ()

@property (nonatomic) int currentQuestionIndex;

@property (nonatomic, copy) NSArray *questions;

@property (nonatomic, copy) NSArray *answers;

@property (nonatomic, weak) IBOutlet UILabel *questionLabel;
@property (nonatomic, weak) IBOutlet UILabel *answerLabel;

@end

@implementation BNRQuizViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    //Call the init method implemented by the superclass
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        //Create two arrays filled with questions and answers and make the pointers point to them
        self.questions = @[@"From what is cognac made?", @"What is 7 + 7?", @"What is the capital of Vermont?"];
        self.answers = @[@"Grapes", @"14", @"Montpelier"];
    
    }
    //return the address of the new object
    return self;
}

- (IBAction) showQuestion:(id)sender
{
    //Step to the next question
    self.currentQuestionIndex++;
    
    //Am I past the last question?
    
    if (self.currentQuestionIndex == [self.questions count])
    {
        self.currentQuestionIndex = 0;
    }
    
    //**Begin moving of the current question, which is in the center of the screen, off to the right of the screen**

    //create a frame that corresponds to the center of the screen where the question is normally displayed
    CGRect frame = CGRectMake(20, 57, 280, 21);

    //Set questionLabel's frame to the above frame.
    self.questionLabel.frame = frame;
    
    //Make sure questionLabel's alpha is 1.0
    self.questionLabel.alpha = 1.0;
    
    //Change the frame rect to the rect where the question will end up when going off to the right side of the screen
    frame = CGRectMake(self.view.bounds.size.width, self.questionLabel.frame.origin.y, 280, 21);
    
    //Animate the alpha to 0
    [UIView animateWithDuration:0.3 animations:^{self.questionLabel.alpha = 0.0;}];

    //Simultaneously animate the question flying off to the right
    [UIView animateWithDuration:0.5 delay:0.0 options:0 animations:^{
        self.questionLabel.frame = frame;
    } completion:^(BOOL finished){
        
        //When the above animation completes, begin the animation of the new question coming in from the left into the middle of screen
        
        //Get the string at the new index in the questions array
        NSString *question = self.questions[self.currentQuestionIndex];

        //Display the string in the question label
        self.questionLabel.text = question;
    
        //Make the new question's rect just off to the screen's left and set its alpha to zero
        self.questionLabel.frame = CGRectMake(0 - self.questionLabel.frame.size.width, self.questionLabel.frame.origin.y, 280, 21);
        self.questionLabel.alpha = 0.0;
    
        
        //animate its alpha change to 1
        [UIView animateWithDuration:0.3 animations:^{self.questionLabel.alpha = 1.0;}];
        
        //simultaneously animate its movement to center of screen
        [UIView animateWithDuration:0.5 delay:0.0 options:0 animations:^{CGRect frame = CGRectMake(20, 57, 280, 21);
            self.questionLabel.frame = frame;
        } completion:^(BOOL finished){
        
            //once completed, animate the current answer moving off to screen right and setting its alpha to zero as long as it's not "???"
            if (![self.answerLabel.text isEqualToString:@"???"]) {
                [UIView animateWithDuration:0.3 animations:^{self.answerLabel.alpha = 0.0;}];
                [UIView animateWithDuration:0.5 animations:^{self.answerLabel.frame =CGRectMake(self.view.bounds.size.width, self.answerLabel.frame.origin.y, 280, 21);} completion:^(BOOL finished){
                    //Once that is done, animate the placeholder "???" answer coming in from screen left to center of screen
                    self.answerLabel.frame = CGRectMake(0 - self.answerLabel.bounds.size.width, self.answerLabel.frame.origin.y, 280, 21);
                    self.answerLabel.text = @"???";
                    [UIView animateWithDuration:0.3 animations:^{self.answerLabel.alpha = 1.0;}];
                    [UIView animateWithDuration:0.5 animations:^{
                        self.answerLabel.frame = CGRectMake(20, 326, 280, 21);}];
                    
                }];

            }
            
        }];
        
    }];
 

}

- (IBAction)showAnswer:(id)sender
{
    
   //Create a rect of where on the screen the answer should normally be shown
    CGRect frame = CGRectMake(20, 326, 280, 21);
    
    //Assign answerLabel this rect
    self.answerLabel.frame = frame;
    
    //Make sure answerLabel's alpha is 1.0
    self.answerLabel.alpha = 1.0;
    
    
    //Animate answerLabel's alpha change to 0
    [UIView animateWithDuration:1.2 animations:^{self.answerLabel.alpha = 0.0;}];
    
    //Simultaneously animate answerLabel (which now contains old question's answer) flying off to the right of the screen
    [UIView animateWithDuration:0.5 delay:0.0 options:0 animations:^{
        self.answerLabel.frame =CGRectMake(self.view.bounds.size.width, self.answerLabel.frame.origin.y, 280, 21);
    } completion:^(BOOL finished){
        
        //When that animation completes, set answerLabel's text to the current question's answer, set its alpha to 0, and set its position to be just to the left of where the screen begins
        self.answerLabel.text = self.answers[self.currentQuestionIndex];
        self.answerLabel.alpha = 0.0;
        self.answerLabel.frame = CGRectMake(0 - self.answerLabel.bounds.size.width, self.answerLabel.frame.origin.y, 280, 21);
        
        //Then animate its alpha change to 1.0, and its movement into the center of the screen.
        [UIView animateWithDuration:0.3 animations:^{self.answerLabel.alpha = 1.0;}];
        [UIView animateWithDuration:0.5 delay:0.0 options:0 animations:^{
            self.answerLabel.frame = CGRectMake(20, 326, 280, 21);} completion:NULL];

    }];

    
}

@end

Does the job. Happy to hear anyone’s suggestions on how to improve this or make it more compact!