rocky90
rocky90

Reputation: 1

Change the speed of an animation halfway through?

I want my app to play an animation when a press a button however I want the speed of the animation to slow down halfway through? Currently my app plays a sound/animation when I press a button, however the last 50% of the frames are too fast and I want it to slow down.

Here is my code:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController {

}
- (IBAction)Button {

    AudioServicesPlaySystemSound(SoundID);

}




-(IBAction)startanimating{
    animatedimage.animationImages = [NSArray arrayWithObjects:
                                     [UIImage imageNamed:@"frames_00.png"],
                                     [UIImage imageNamed:@"frames_05.png"],
                                     [UIImage imageNamed:@"frames_10.png"],
                                     [UIImage imageNamed:@"frames_15.png"],
                                     [UIImage imageNamed:@"frames_20.png"],
                                     [UIImage imageNamed:@"frames_25.png"],
                                     [UIImage imageNamed:@"frames_30.png"],
                                     [UIImage imageNamed:@"frames_35.png"],
                                     [UIImage imageNamed:@"frames_40.png"],
                                     [UIImage imageNamed:@"frames_45.png"],
                                     [UIImage imageNamed:@"frames_50.png"],
                                     [UIImage imageNamed:@"frames_50.png"],
                                     [UIImage imageNamed:@"frames_45.png"],
                                     [UIImage imageNamed:@"frames_40.png"],
                                     [UIImage imageNamed:@"frames_35.png"],
                                     [UIImage imageNamed:@"frames_30.png"],
                                     [UIImage imageNamed:@"frames_25.png"],
                                     [UIImage imageNamed:@"frames_20.png"],
                                     [UIImage imageNamed:@"frames_15.png"],
                                     [UIImage imageNamed:@"frames_10.png"],
                                     [UIImage imageNamed:@"frames_05.png"],
                                     [UIImage imageNamed:@"frames_00.png"],nil];




    [animatedimage setAnimationRepeatCount:1];
    animatedimage.animationDuration = 0.7;
    [animatedimage startAnimating];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    NSURL *buttonURL = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"AVTREV" ofType:@"mp3"]];

    AudioServicesCreateSystemSoundID((__bridge CFURLRef)buttonURL, &SoundID);
}

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

@end

Frame 50 is where I want it to play at a much slower speed. Any help would be fantastic thank you!

Upvotes: 0

Views: 157

Answers (1)

user3386109
user3386109

Reputation: 34829

With the current code you have an array with 22 items in it. To achieve the desired effect, you can use an array with 33 items in it, by duplicating each item after frame_50, so that the entries in the array look like this

00 05 10 ... 45 50 50 50 45 45 40 40 ... 05 05 00 00

Since the filenames follow a predictable pattern, you can even use a loop to create the array.

int i;
NSString *name;
NSMutableArray *tempArray = [NSMutableArray new];
for ( i = 0; i <= 50; i += 5 )
{
    name = [NSString stringWithFormat:@"frames_%02d.png", i];
    [tempArray addObject:[UIImage imageNamed:name]];
}
for ( i = 50; i >= 0; i -= 5 )
{
    name = [NSString stringWithFormat:@"frames_%02d.png", i];
    [tempArray addObject:[UIImage imageNamed:name]];
    [tempArray addObject:[UIImage imageNamed:name]];    // add the frame twice
}
animatedImage.animationImages = tempArray;

edit -- The code above is intended to replace the array initialization code

animatedimage.animationImages = [NSArray arrayWithObjects:
                                     [UIImage imageNamed:@"frames_00.png"],
                                     [UIImage imageNamed:@"frames_05.png"],
                                     ...

Upvotes: 3

Related Questions