Change image during translation

How can I change myLayer.contents with a set of images (i.e. switching between img1 and img2) during translation? thank!

UIImage *myImage = [UIImage imageNamed:@"img1.png"];
CALayer *myLayer = [CALayer layer];
myLayer.contents = (id)myImage.CGImage;
myLayer.Position = CGPointMake(0,0);
myLayer.Bounds=CGRectMake(0.0, 0.0, 50, 50);
[self.view.layer addSublayer:myLayer];

//translation1
CGPoint startPt = CGPointMake(10,10);
CGPoint endPt = CGPointMake(100,100);    
CABasicAnimation *transl1 = [CABasicAnimation animationWithKeyPath:@"position"];
transl1.removedOnCompletion = FALSE;
transl1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
transl1.fromValue = [NSValue valueWithCGPoint:startPt];
transl1.toValue = [NSValue valueWithCGPoint:endPt];
transl1.duration = 3;
transl1.fillMode = kCAFillModeForwards;
transl1.beginTime = 0;
[myLayer addAnimation: transl1 forKey:  nil];
+5
source share
3 answers

Example of a walking person:

, , 6 12 . , , . , ( , ) animationImages UIImageView. . , . , , .

, :

, .

animationImagesSpider = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1@2x.png"], [UIImage imageNamed:@"2@2x.png"], [UIImage imageNamed:@"3@2x.png"], [UIImage imageNamed:@"4@2x.png"], [UIImage imageNamed:@"5@2x.png"], [UIImage imageNamed:@"6@2x.png"], [UIImage imageNamed:@"6@2x.png"], [UIImage imageNamed:@"8@2x.png"], [UIImage imageNamed:@"9@2x.png"], [UIImage imageNamed:@"10@2x.png"], [UIImage imageNamed:@"11@2x.png"], [UIImage imageNamed:@"1@2x.png"], nil];   

UIImageView:

imgViewSpider = [[UIImageView alloc] initWithFrame:CGRectMake(200,410,100,145)];
imgViewSpider.animationImages = animationImagesSpider;

, [imgViewSpider startAnimating]; , . , , , :

- (void) spiderRun {

    imgViewSpider.animationDuration= 0.51-(accSp/3.5);
    [imgViewSpider setAnimationRepeatCount:222]; /// this is a dummy value that has no effect because animtion ends after the first frame
    [imgViewSpider startAnimating];
    [self performSelector:@selector(spiderRun) withObject:nil afterDelay: 0.5-(accSp/3.5)];

}

accSp, .

+2

, , .

. . , .

beginTime duration.

+1

You will need to create a second animation transl2, this animation will make the exchange of images. You will need to set the duration to half the duration of the translation so that it falls in the middle of the animation transl1.

transl1.duration = 3

transl2.duration = 1.5

0
source

All Articles