xcode iPhone 开发:动画 PNG 序列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2734669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:19:05  来源:igfitidea点击:

iPhone Dev: Animating PNG Sequences

iphoneobjective-cxcodeoopanimation

提问by Franky

What is the best or recommended technique for animating PNG Sequences.
Heres what I've learned:

什么是动画 PNG 序列的最佳或推荐技术。
这是我学到的:

Do it Manually
Using MutableArrays containing Strings, you can animate a UIImageView with a timer which increments an index number


使用包含字符串的 MutableArrays手动执行,您可以使用一个递增索引号的计时器为 UIImageView 设置动画

UIImage - animation methods
This works, the only problem is to find if an image has completed its animation, you have to check the isAnimating BOOL, and to do that you need a timer.

UIImage - 动画方法
这是有效的,唯一的问题是要查找图像是否已完成其动画,您必须检查 isAnimating BOOL,为此您需要一个计时器。

What is the best and recommended?

什么是最好的和推荐的?

Looking to do Oldschool game sprite animations,ie:

希望做 Oldschool 游戏精灵动画,即:

Idle Animation
Attack Animation
Walk Animation
ect...

Let me know if any one has something.

空闲动画
攻击动画
步行动画等
...

让我知道如果有人有什么。

@lessfame

@lessfame

回答by Kirill SIMAGIN

Example to animate arrows

动画箭头的示例

    UIImage* img1 = [UIImage imageNamed:@"fleche1.png"];
    UIImage* img2 = [UIImage imageNamed:@"fleche2.png"];

    NSArray *images = [NSArray arrayWithObjects:img1,img2, nil];
    UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 160.0, 160.0)];
    [imageView setAnimationImages:images];
    [imageView setAnimationRepeatCount:100];
    [imageView setAnimationDuration:3.0];
    imageView.center = myView.center;


    [imageView startAnimating];
    [myView addSubview:imageView];
    [imageView release];

回答by Ramin

Easiest way is to use Core Animation layers to do sprite animation:

最简单的方法是使用 Core Animation 层来做精灵动画:

  • Make a 'multi-cell' image strip (could be PNG or JPG) of all the various moves for the sprite. Make each 'cell' be a fixed height or width.

  • Put the image in a UIImageView.

  • Take the CALayer of the view, and adjust the contentsRectproperty of the layer. This acts as a clipping rectangle for the sprite. To animate sprite all you have to do is move the contentsRectover to the next cell position in the image strip.

  • 为精灵的所有各种动作制作一个“多单元”图像条(可以是 PNG 或 JPG)。使每个“单元格”具有固定的高度或宽度。

  • 将图像放在 UIImageView 中。

  • 取视图的CALayer,调整contentsRect图层的属性。这充当精灵的剪切矩形。要为精灵设置动画,您所要做的就是将contentsRect上方移动到图像条中的下一个单元格位置。

Something like this will do it (assuming you've already calculated the newSpritePosition.

像这样的事情会做到这一点(假设您已经计算了 newSpritePosition.

  [CATransaction begin];
  [CATransaction setDisableActions:YES];
  spriteLayer.contentsRect = newSpritePosition;
  [CATransaction commit];

(If you don't do the second line, then you'll get a default slide animation instead of a 'jump' to the next state.)

(如果你不做第二行,那么你会得到一个默认的幻灯片动画,而不是“跳转”到下一个状态。)

With this technique you can rotate through all the frames of the sprite -- much like a flipbook. CALAyer transactions are optimized so you should get pretty fast frame rates. You can set an animationDidStopcompletion handler to move to the next state of the sprite or loop back to the beginning frame.

使用这种技术,您可以旋转精灵的所有帧——就像翻书一样。CALAyer 事务经过优化,因此您应该获得非常快的帧速率。您可以设置animationDidStop完成处理程序以移动到精灵的下一个状态或循环回到起始帧。

回答by Jason Webb

It depends on how you are going to use the sprites. If you just need a simple looping sprite then the UIImage method is great. If you want more granular control then you will be happier loading the images into an array and cycling them using a NSTimerto handle the timing. Personally I use the array method most often because it leaves me more options in the future (like detecting when animations have completed). One more suggestion would be to check out the cocos2dproject. Its sprite is alot more powerful than either of these suggestions.

这取决于您将如何使用精灵。如果您只需要一个简单的循环精灵,那么 UIImage 方法就很棒。如果您想要更精细的控制,那么您将更乐意将图像加载到数组中并使用 a 循环它们NSTimer来处理时间。我个人最常使用数组方法,因为它为我将来留下了更多选择(例如检测动画何时完成)。另一个建议是查看cocos2d项目。它的精灵比这些建议中的任何一个都强大得多。

回答by cregox

As I said in another answer, I found this to be a good way: PNG Animation method by Moses DeJong

正如我在另一个答案中所说,我发现这是一个好方法:Moses DeJong 的 PNG Animation method

From his words:

从他的话来看:

This example implements an animation oriented view controller that simply waits to read the PNG image data for a frame until it is needed. Instead of alllocating many megabytes, this class run in about a half a meg of memory with about a 5-10% CPU utilization on a 2nd gen iPhone.

这个例子实现了一个面向动画的视图控制器,它只是等待读取帧的 PNG 图像数据,直到需要它为止。这个类不是分配很多兆字节,而是在大约半兆内存中运行,在第二代 iPhone 上的 CPU 利用率约为 5-10%。

I'm still unsure exactly how it's done. I believe it basically uses UIImageView to cache up AVAudioPlayer.

我仍然不确定它是如何完成的。我相信它基本上使用 UIImageView 来缓存 AVAudioPlayer。