【问题标题】:Change the speed of an animation halfway through?中途改变动画的速度?
【发布时间】:2014-03-14 05:36:14
【问题描述】:

我希望我的应用在按下按钮时播放动画,但是我希望动画的速度在中途减慢?目前,当我按下按钮时,我的应用会播放声音/动画,但是最后 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 帧是我希望它以更慢的速度播放的地方。任何帮助都会非常感谢您!

【问题讨论】:

  • animatedimageUIImageView?
  • 把它分解成两个动画?一个速度为 100%,另一个速度为 50%
  • @user1846108 我该怎么做这到底是什么最好的方法?

标签: ios animation uiimageview


【解决方案1】:

使用当前代码,您有一个包含 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;

edit -- 上面的代码是为了替换数组初始化代码

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

【讨论】:

  • 感谢您的帮助,但我很迷茫。我要删除所有当前的数组帧吗?我在哪里放下面的代码?
  • 其实我现在有点明白你的意思了。关于复制帧,这真的很聪明。
  • @rocky90 抱歉,您的第一条消息没有收到通知。我已经添加到我的答案中,但听起来你已经想通了:)
  • 谢谢,我也遇到了更多麻烦。我正在尝试制作一个应用程序,它是一个速度计,当您单击一个按钮时,它会像真正的汽车一样移动速度计。唯一的问题是我想要的是连续按两次而不是再次重置播放动画。我希望它基本上移动两倍?
  • 重申我想要的基本上是“按住按钮 0.5 秒播放动画帧 1,2,3,4,5 但按住 1 秒播放动画帧 1,2,3,4,5 ,6,7,8,9,10。连续按两次按钮 0.5 秒播放 1,2,3,4,5,6,7,8,9,10。同样。@user3386109
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多