pas*_*aya 2 animation objective-c uiimageview cabasicanimation ios
标题可能不是那么清楚,但我想要做的是让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
Run Code Online (Sandbox Code Playgroud)
这是我使用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"];
Run Code Online (Sandbox Code Playgroud)
所以当UIImageView的位置被动画化时,gif效果不起作用.我的问题是如何才能使它成为UIImageView循环通过一系列图像,同时它的位置是动画的?
这是猜测,我根本没有测试过这个想法,但是你是否考虑过以同样的方式实现图像动画,你正在使用CAKeyframeAnimation
?您需要CGImage
从数组中构造一个对象UIImage
数组(以设置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];
}
Run Code Online (Sandbox Code Playgroud)