中途改变动画的速度?

时间:2014-03-12 23:41:09

标签: ios animation uiimageview

我希望我的应用程序在按下按钮时播放动画,但是我想让动画的速度减慢一半?目前我的应用程序在按下按钮时会播放声音/动画,但最后50%的帧速度太快,我希望它减速。

这是我的代码:

#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

第50帧是我希望它以更慢的速度播放的地方。任何帮助都会非常棒,谢谢!

1 个答案:

答案 0 :(得分:3)

使用当前代码,您有一个包含22个项目的数组。为了达到预期的效果,您可以使用包含33个项目的数组,方法是复制frame_50之后的每个项目,以便数组中的条目看起来像这样

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

由于文件名遵循可预测的模式,您甚至可以使用循环来创建数组。

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;

编辑 - 上面的代码旨在替换数组初始化代码

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