TheGrayVacuum
TheGrayVacuum

Reputation: 713

iOS: How to make animation play 3 times then end?

I found this code in a tutorial online. It plays an animation in an endless loop.

I want to make an animation that plays three times then ends. Right now I have a looping animation. This is a very simple question but as I am brand new to iOS.

#import "APOViewController.h"

@interface APOViewController ()

@end

@implementation APOViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *imageNames = @[@"win_1.png", @"win_2.png", @"win_3.png", @"win_4.png",
                            @"win_5.png", @"win_6.png", @"win_7.png", @"win_8.png",
                            @"win_9.png", @"win_10.png", @"win_11.png", @"win_12.png",
                            @"win_13.png", @"win_14.png", @"win_15.png", @"win_16.png"];

    NSMutableArray *images = [[NSMutableArray alloc] init];
    for (int i = 0; i < imageNames.count; i++) {
        [images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
    }

    // Normal Animation
    UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 80, 86, 193)];
    animationImageView.animationImages = images;
    animationImageView.animationDuration = 0.5;

    [self.view addSubview:animationImageView];
    [animationImageView startAnimating];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


}

@end

Thanks. I tried to combine the 2 different answers I received so far. But I'm still getting an error "No visible @interface for 'UIImageView' declares the selector 'animationRepeatCount:'

Here is what I have changed the code to:

#import "APOViewController.h"
#import "UIKit/UIImageView.h"

@interface APOViewController ()
@property(nonatomic) NSInteger animationRepeatCount;

@end

@implementation APOViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *imageNames = @[@"win_1.png", @"win_2.png", @"win_3.png", @"win_4.png",
                            @"win_5.png", @"win_6.png", @"win_7.png", @"win_8.png",
                            @"win_9.png", @"win_10.png", @"win_11.png", @"win_12.png",
                            @"win_13.png", @"win_14.png", @"win_15.png", @"win_16.png"];

    NSMutableArray *images = [[NSMutableArray alloc] init];
    for (int i = 0; i < imageNames.count; i++) {
        [images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
    }

    // Normal Animation
    UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 80, 86, 193)];
    animationImageView.animationImages = images;
    animationImageView.animationDuration = 0.5;

    [self.view addSubview:animationImageView];

    [animationImageView startAnimating];
    [animationImageView animationRepeatCount:3];
    [animationImageView stopAnimating];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


}

@end

Upvotes: 1

Views: 136

Answers (2)

Gago
Gago

Reputation: 307

Use property of UIImageView @property(nonatomic) NSInteger animationRepeatCount;

Upvotes: 1

67cherries
67cherries

Reputation: 6951

Do this:

[animationImageView setRepeatCount:3];

Upvotes: 1

Related Questions