我通过将 UIImage 对象数组传递给下面列出的动画方法,在我的 iOS 应用程序中使用 UIImageView.animationImages 制作了很多动画。当前我的 UIImage 对象数组包含单独的 PNG 文件。我知道我可以通过使用 & 传递 Sprite Sheet 而不是传递一组单独的 PNG 文件来制作类似的动画。一个比另一个有什么优势,或者 Sprite Sheet 用于不同的目的?
- (void) startImageViewAnimation:(UIImageView *)imageView WithImages:(NSArray*)imageArray orBuildArrayWithImage:(NSString *)imageName duration:(CGFloat)duration repeatCount:(NSInteger)repeatCount soundFile:(NSString *)soundFile stopSoundAfterDuration:(CGFloat) stopSoundDuration
{
if([imageName length] != 0)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
UIImage *image;
int index = 1;
NSString *string;
do
{
string = [NSString stringWithFormat:@"%@%02d.png", imageName, index++];
image = [UIImage imageNamed:string];
if(image)
[array addObject:image];
} while (image != nil);
imageView.animationImages = array;
[array release];
}
else
{
imageView.animationImages = imageArray;
}
imageView.animationDuration = duration;
imageView.animationRepeatCount = repeatCount;
[imageView startAnimating];
if([soundFile length] != 0)
{
[self loadSoundEffectAudio:soundFile];
}
}
最佳答案
Sprite 表仅适用于非常小的动画,其中动画的所有帧都可以放入加载到内存中的 1 个“表”中,然后使用 diff x,y 偏移来访问不同的帧数据。如果您有更长的动画或更大的动画,那么 Sprite 表就没有任何值(value)。过去,角色 Sprite 相当小,因此其中很多可以放在一张纸上,然后可以快速制作像拳击这样的短动画,因为不需要将内存从一帧交换到下一帧。除了最小的小动画之外,你不应该使用 UIImageView.animationImages ,因为它会耗尽内存并在与合理大小的中等大小动画一起使用时使你的设备崩溃。关于更好方法的更多详细信息可以在我对这个问题的回答中找到 How to do animations using images efficiently in iOS
关于ios - 用于动画的 Sprite 表与 PNG 文件数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11566980/