JohnBigs
JohnBigs

Reputation: 2811

How to create a loop to specify different actions every time a button pushed?

I have a method in my view controller that called 'getQuoteButton' which is a IBAction method that attached to a button, and every time I tap on the button I'm getting a random quote.

I have created a certain push animation for each time I'm presenting a new quote, and I also want to create a loop that I can change the animation each time I tap on the button but I don't know how..

this is my code(NOViewController), it's a single view app:

 #import "NOViewController.h"
#include "NOQuotes.h"

@interface NOViewController ()

@end

@implementation NOViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.quotes = [[NOQuotes alloc] init];

    UIImage *background = [UIImage imageNamed:@"albert"];

    [self.backgroundImageView setImage:background];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)getQuoteButton
{

    self.quotesLabel.text = [self.quotes getRandomQuote];

    CATransition *animationTran = [CATransition animation];
    [animationTran setType:kCATransitionPush];
    [animationTran setDuration:0.7f];
    [animationTran setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [self.quotesLabel.layer addAnimation:animationTran forKey:@"pushAnimation"];

}


@end

Would appreciate any help, and let me know if you need any other info so you can help :)

Upvotes: 1

Views: 97

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

Add an integer counter variable, and increment it each time the button is pushed. Add code to getQuoteButton that checks the counter, and sets animation type and subtype accordingly:

@interface NOViewController () {
    int numClicks; // <<=== Added
}

@end

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.quotes = [[NOQuotes alloc] init];

    UIImage *background = [UIImage imageNamed:@"albert"];

    [self.backgroundImageView setImage:background];
    numClicks = 0; // <<=== Added
}

- (IBAction)getQuoteButton
{

    self.quotesLabel.text = [self.quotes getRandomQuote];

    CATransition *animationTran = [CATransition animation];
    switch (numClicks) { // <<=== Added
        case 0:
            [animationTran setType:kCATransitionPush];
            [animationTran setSubtype:kCATransitionFromRight];
            break;
        case 1:
            [animationTran setType:kCATransitionPush];
            [animationTran setSubtype:kCATransitionFromLeft];
            break;
        case 2:
            [animationTran setType:kCATransitionPush];
            [animationTran setSubtype:kCATransitionFromTop];
            break;
        ... // And so on;
        case 13:
            [animationTran setType:kCATransitionFade];
            // Fade has no subtype
            break;
    }
    numClicks = (numClick+1) % 13; // <<=== Added
    [animationTran setDuration:0.7f];
    [animationTran setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [self.quotesLabel.layer addAnimation:animationTran forKey:@"pushAnimation"];
}

You can shorten this code by creating two arrays or an array of type/subtype pairs, but the idea of incrementing a member variable on click is what you need to implement this.

Upvotes: 1

Related Questions