CAKeyframeAnimation keyTimes设置

时间:2015-03-17 22:14:43

标签: ios objective-c core-animation cakeyframeanimation

这是我的代码:

- (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];,那么它会完美播放。我在这里做错了什么?

1 个答案:

答案 0 :(得分:4)

我在回答你关于同一主题的另一个问题时已经解释了这一点。

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

在您发布的代码中,您根本不指定任何持续时间。动画的默认持续时间必须短于.2秒,因此当您不提供持续时间时,您的持续时间非常短。如果指定较长的持续时间,则运行速度较慢。

假设默认持续时间为.2秒。您的keyTimes值因.04而异 .2 * .04 = .008秒,或千分之八秒。如果我正确并且动画的默认持续时间为.2秒,则表示每帧为.008秒。任何短于.01666秒/帧(1/60秒)的东西都是没有意义的,因为动画必须跳过帧。

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