简体   繁体   English

半途改变动画的速度?

[英]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. 目前,当我按下一个按钮时,我的应用会播放声音/动画,但是最后50%的帧太快了,我希望它放慢速度。

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. 我希望第50帧的播放速度要慢得多。 Any help would be fantastic thank you! 任何帮助都将非常棒,谢谢!

With the current code you have an array with 22 items in it. 使用当前代码,您将拥有一个包含22个项目的数组。 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 为了达到预期的效果,可以在其中包含33个项目的数组中使用,方法是在frame_50之后复制每个项目,以使数组中的条目看起来像这样

00 05 10 ... 45 50 50 50 45 45 40 40 ... 05 05 00 00 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"],
                                     ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM