Tulon
Tulon

Reputation: 4124

CAKeyframeAnimation keyTimes set up

This is my code:

- (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];
}

The animation will happen in a fixed UIImageView. The time interval between frames is 0.04. If I run this, the animation playing with a fast speed. But If I set the duration like this [anim setDuration:0.76];, It plays perfectly. What's the thing I did wrong here?

Upvotes: 1

Views: 764

Answers (1)

Duncan C
Duncan C

Reputation: 131418

I already explained this in an answer to your other question on the same subject.

The times in key times are fractions of the total interval of the animation, not fractions of a second.

In the code you posted you don't specify any duration at all. The default duration for an animation must be something short like .2 seconds, so when you don't provide a duration, you get a very short duration. If you specify a longer duration, it runs slower.

Say the default duration is .2 seconds. Your keyTimes values vary by .04, and .2 * .04 = .008 seconds, or 8 thousandths of a second. If I'm right and the default duration of an animation is .2 seconds, you are giving each frame .008 seconds. Anything shorter than .01666 seconds/frame (1/60th of a second) is pointless, as the animation will have to skip frames.

In fact, since the docs don't specify a default duration, the results when you don't specify a duration are probably undefined.

Upvotes: 4

Related Questions