gpt4 book ai didi

ios - 如何同时为 UIImageView 的图像和 UIImageView 本身设置动画?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:03:15 26 4
gpt4 key购买 nike

标题可能不太清楚,但我想做的是让 UIImageView 显示一系列图像(有点像 gif),我通过将 animationImages 设置为图像数组,然后调用 [imageView startAnimating];。这很好用。我也有使用 CABasicAnimation 移动 UIImageView 的代码,并且此动画代码也可以正常工作。但是,当我尝试为 UIImageView 的图像设置动画并尝试移动 UIImageView 时,UIImageView 的图像停止动画。有解决方法吗?

这是我为 UIImageView 的内容设置动画的代码:

    self.playerSprite = [[UIImageView alloc] initWithImage:[self.player.animationImages objectAtIndex:0]];
[self.playerSprite setFrame:CGRectMake(self.center.x, self.center.y, self.tileSet.constants.TILE_WIDTH, self.tileSet.constants.TILE_HEIGHT)];
self.playerSprite.animationImages = self.player.animationImages;
self.playerSprite.animationDuration = self.tileSet.constants.animationDuration;
self.playerSprite.animationRepeatCount = 0; //Makes it repeat indefinitely

这是我使用 CABasicAnimation 为 UIImageView 设置动画的代码:

float playerSpriteX = self.playerSprite.center.x;
float playerSpriteY = self.playerSprite.center.y;

CABasicAnimation *moveAnimation = [CABasicAnimation animation];
moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(playerSpriteX + TILE_WIDTH, playerSpriteY)];
[moveAnimation setDelegate:self];
[moveAnimation setFillMode:kCAFillModeForwards];
[moveAnimation setRemovedOnCompletion:NO];
[moveAnimation setDuration:MOVE_ANIMATION_DURATION];
[self.playerSprite.layer addAnimation:moveAnimation forKey:@"position"];

因此,当 UIImageView 的位置处于动画状态时,gif 效果不起作用。我的问题是我怎样才能使 UIImageView 在其位置动画时循环显示图像数组?

最佳答案

这是推测,我根本没有测试过这个想法,但是您是否考虑过以与为位置设置动画相同的方式使用 CAKeyframeAnimation 来实现图像动画?您需要从 UIImage 数组构造一个 CGImage 对象数组(以设置关键帧动画的 values 属性),但它看起来就像一个非常简单的转换:

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];
}

关于ios - 如何同时为 UIImageView 的图像和 UIImageView 本身设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16025769/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com