CAKeyframeAnimation keyTimes 设置

CAKeyframeAnimation keyTimes set up

这是我的代码:

- (void)animateImages
{
    self.emoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 160, 50, 50)];
    self.emoImageView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:emoImageView];

    NSArray *values = [NSArray arrayWithObjects:
                       (id)[UIImage imageNamed:@"smiley64_1_1.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_2.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_3.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_4.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_5.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_6.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_7.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_8.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_9.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_10.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_11.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_12.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_13.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_14.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_15.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_16.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_17.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_18.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_19.png"].CGImage,
                       (id)[UIImage imageNamed:@"smiley64_1_20.png"].CGImage, nil];


    NSMutableArray *keyTimes = [NSMutableArray arrayWithObjects:
                            [NSNumber numberWithFloat:0.00f],
                            [NSNumber numberWithFloat:0.04f],
                            [NSNumber numberWithFloat:0.08f],
                            [NSNumber numberWithFloat:0.12f],
                            [NSNumber numberWithFloat:0.16f],
                            [NSNumber numberWithFloat:0.20f],
                            [NSNumber numberWithFloat:0.24f],
                            [NSNumber numberWithFloat:0.28f],
                            [NSNumber numberWithFloat:0.32f],
                            [NSNumber numberWithFloat:0.36f],
                            [NSNumber numberWithFloat:0.40f],
                            [NSNumber numberWithFloat:0.44f],
                            [NSNumber numberWithFloat:0.48f],
                            [NSNumber numberWithFloat:0.52f],
                            [NSNumber numberWithFloat:0.56f],
                            [NSNumber numberWithFloat:0.60f],
                            [NSNumber numberWithFloat:0.64f],
                            [NSNumber numberWithFloat:0.68f],
                            [NSNumber numberWithFloat:0.72f],
                            [NSNumber numberWithFloat:0.76f], nil];


    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    [anim setKeyTimes:keyTimes];
    [anim setValues:values];
    [anim setKeyPath:@"contents"];
    //[anim setCalculationMode:@"discrete"];
    [anim setRepeatCount: 90000];
    [self.emoImageView.layer addAnimation:anim forKey:nil];
}

动画将以固定的 UIImageView 发生。帧之间的时间间隔为 0.04。如果我运行这个,动画播放的速度很快。但是如果我像这样设置持续时间 [anim setDuration:0.76];,它会完美播放。我在这里做错了什么?

我已经在回答您关于同一主题的其他问题时对此进行了解释。

关键时间中的时间是动画总间隔的分数,而不是秒的分数。

在您发布的代码中,您根本没有指定任何持续时间。动画的默认持续时间必须很短,例如 0.2 秒,因此当您不提供持续时间时,您将获得非常短的持续时间。如果您指定更长的持续时间,它会运行得更慢。

假设默认持续时间为 0.2 秒。您的 keyTimes 值相差 0.04,并且 .2 * .04 = .008 秒,或千分之八秒。如果我是对的并且动画的默认持续时间是 0.2 秒,那么您给每一帧 0.008 秒。任何短于 .01666 seconds/frame(1/60 秒)的东西都是没有意义的,因为动画将不得不跳帧。

事实上,由于文档未指定默认持续时间,因此您未指定持续时间时的结果可能未定义。