views:

53

answers:

2

How would i go about creating an image sequence with core animation. I would like to:

add image1 for 1 second then remove image

add image2 for 2 seconds then remove image

add image1 for 3 seconds then remove

    CGImageRef image1 = [self getImage1];
    CALayer *image1Layer = [CALayer layer];
    image1Layer.bounds = CGRectMake(0, 0, 480, 320);
    image1Layer.position = CGPointMake(0, 0);
    image1Layer.contents = (id)image1;


    CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"animation"];
    animation1.repeatCount = 0; 
    animation1.duration = 2.0;
    animation1.removedOnCompletion = YES; // i would like to remove image here
    animation1.beginTime = AVCoreAnimationBeginTimeAtZero; 
    [image1Layer addAnimation:animation1 forKey:nil];

The above code adds an image but does not remove it.

Cheers

+1  A: 

The easiest way is to use CABasicAnimation for contents key:

CABasicAnimation *animation = [CABasicAnimation animation];
animation.fromValue = (id)[UIImage imageNamed:@"image1.png"].CGImage;
animation.toValue = (id)[UIImage imageNamed:@"image2.png"].CGImage;
animation.duration = 1.0f;
animation.repeatCount = HUGE_VAL;
//  animation.autoreverses = YES;
[image1Layer addAnimation:animation forKey:@"contents"];

This animation will infinitely change the layer contents between image1 and image2. You may want to set autoreverses property for smoother transitions - test animation either ways and choose the option you like the best.

Vladimir
I am getting the error: cannot convert 'CGImage*' to 'objc_object*' in argument passing. And i would like to add about ten images and cut straight from one to the other. Thanks for the answer.
sorry, you need to explicitly cast cgimage to id - that should fix the error. To animate through 10 images you can use CAKeyFrameAnimation in a way similar to CABasicAnimation, you'll only need to provide an array of images to iterate through and array of keyTimes to control animations timing
Vladimir
Hey Vladimir, could you please have a look at the below code and give me some guidance on what i may be doing wrong.Thanks again for your help
A: 

Can't seem to get it working, what am i doing wrong.

    CAKeyframeAnimation *customFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"frame"];
    NSArray *sizeValues = [NSArray arrayWithObjects:(id)im1.CGImage, (id)im2.CGImage, nil];
    NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:2.5f], nil]; 
    NSArray *timingFunctions = [NSArray arrayWithObjects: [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], nil];

    [customFrameAnimation setValues:sizeValues];
    [customFrameAnimation setKeyTimes:times];

    customFrameAnimation.duration=10.0;
    customFrameAnimation.fillMode = kCAFillModeForwards;
    customFrameAnimation.timingFunctions = timingFunctions;
    customFrameAnimation.removedOnCompletion = NO;
    [animatedTitleLayer addAnimation:customFrameAnimation forKey:nil]; 
You should not post that in an answer - edit your initial question or open new question. The problem with this code is that times should contain values between 0 and 1 and indicate times relative to animation duration (say, 0.5 value will mean at the middle of animation duration)
Vladimir
Got it sorted. I really appreciate you taking the time to help me.Cheers