How do I simultaneously animate a UIImageView's image and the UIImageView itself?

后端 未结 4 1947
余生分开走
余生分开走 2020-12-16 08:26

The title may not be so clear, but what I want to do is to make the UIImageView display a series of images (sort of like a gif) and I do this by setting the animationI

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 09:00

    This is speculation and I haven't tested this idea at all, but have you considered implementing the image animation in the same fashion you're animating the position, with a CAKeyframeAnimation? You'd need to construct an array of CGImage objects from your UIImage array (to set the values property of the keyframe animation) but it looks like a pretty straightforward conversion:

    CAKeyframeAnimation *imageAnimation = [CAKeyframeAnimation animation];
    imageAnimation.calculationMode = kCAAnimationDiscrete; // or maybe kCAAnimationPaced
    imageAnimation.duration = self.tileSet.constants.animationDuration;
    imageAnimation.repeatCount = HUGE_VALF;
    // the following method will need to be implemented to cast your UIImage array to CGImages
    imageAnimation.values = [self animationCGImagesArray];
    [self.playerSprite.layer addAnimation:imageAnimation forKey:@"contents"];
    
    -(NSArray*)animationCGImagesArray {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self.player.animationImages count]];
        for (UIImage *image in self.player.animationImages) {
            [array addObject:(id)[image CGImage]];
        }
        return [NSArray arrayWithArray:array];
    }
    

提交回复
热议问题