Search code examples
iphoneobjective-cioscalayercabasicanimation

CABasicAnimation with array of frames


I have a CABasicAnimation with a .fromValue and a .toValue that move in an PNG image with 202 images and display them one by one

also i used CALayer to hold the image.png

the problem that it pass all the images by sequence i want to display the images by frames that i will put in an NSArray

here is my code:

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"sampleIndex"];
anim.fromValue = [NSNumber numberWithInt:1];
anim.toValue = [NSNumber numberWithInt:203];
anim.duration = 1.75f;
anim.repeatCount = HUGE_VALF;
anim.autoreverses = YES; 
[myImage addAnimation:anim forKey:nil];

and the array of frames is:

frames =   (753, 377, 798, 422)  ,
  (706, 377, 751, 422)  ,
  (659, 377, 704, 422)  ,
  (612, 377, 657, 422)  ,
  (565, 377, 610, 422)  ,
  (518, 377, 563, 422)  ,
  (518, 424, 563, 468)  ,
  (471, 424, 516, 468)  ,
  (424, 424, 469, 468)  ,
  (471, 377, 516, 422)  ,
  (424, 377, 469, 422)  ,
  (377, 377, 422, 422)  ,
  (330, 377, 375, 422)  ,

and so on ....

Any Ideas ??


Solution

  • Similarly to your frame array you can add all the needed images(frames) to UIImageView. Here is the example:

    NSArray *images = [NSArray arrayWithObjects: 
                  [UIImage imageNamed:@"frame753.png"],
                  [UIImage imageNamed:@"frame377.png"],
                  [UIImage imageNamed:@"frame798.png"], 
    
                  nil];
    yourImageView.animationImages = images; 
    yourImageView.animationDuration = 1.75; 
    yourImageView.animationRepeatCount = 0;
    [yourImageViewstartAnimating]; 
    

    Hope this helps.