Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Animation Image sequence

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

like image 381
user346443 Avatar asked Dec 28 '22 06:12

user346443


1 Answers

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.

like image 184
Vladimir Avatar answered Jan 05 '23 18:01

Vladimir