按顺序使用图像数组的动画
我有一组图像,我想通过按顺序播放这些图像来制作动画。我想将整个循环重复几次。我正在为 iPad 开发一款游戏。向我建议一种使用 Cocoa 框架在 Objective-C 中实现此功能的方法。
I have an array of images which I want to animate by playing these images one after the other in a sequence. I want to repeat the whole loop several times. I am developing a game for iPad. Suggest to me a method to achieve this functionality in Objective-C with the Cocoa framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在数组中添加您自己的图像。repeat Count 0 表示无限循环。您也可以给出自己的编号。
add your own images in the array.repeat Count 0 means infinite loop.You can give your own number also.
至少有 3 种方法可以通过
UIImageView
对图像数组进行动画处理。我添加了 3 个链接来下载 3 种可能性的示例代码。第一个是大家都知道的。其他的则鲜为人知。
- UIImageView.animationImages
示例链接
这个的问题是没有 Delegate 告诉我们动画在哪一刻完成。因此,如果我们想在动画之后显示某些内容,可能会遇到问题。
同样,不可能自动将动画中的最后一个图像保留在
UIImageView
中。如果我们将这两个问题结合起来,如果我们想将最后一帧保留在屏幕上,我们可能会在动画的结尾处出现间隙。- CAKeyframeAnimation
示例链接
这种动画方式通过
CAAnimation
进行工作。它有一个易于使用的委托,我们可以知道动画何时完成。这可能是对图像数组进行动画处理的最佳方式。
委托:
- CADisplayLink
示例链接
CADisplayLink 对象是一个计时器对象,允许您的应用程序将其绘制与刷新同步显示速率。
这种方法非常有趣,并且为操纵我们在屏幕上显示的内容提供了很多可能性。
DisplayLink getter:
方法:
一般问题:
尽管我们有这 3 种可能性,但如果您的动画包含大量大图像,请考虑使用视频。内存的使用量会减少很多。
执行此操作时您将遇到的一个常见问题是在分配图像时。
如果您使用 [UIImage imageNamed:@"imageName"] 您将遇到缓存问题。
来自 Apple:
此方法在系统缓存中查找具有指定名称的图像对象,并返回该对象(如果存在)。如果缓存中尚不存在匹配的图像对象,则此方法将从磁盘或资产类别中查找并加载图像数据,然后返回结果对象。您不能假设此方法是线程安全的。
因此,
imageNamed:
将图像存储在私有缓存中。- 第一个问题是你无法控制缓存大小。
- 第二个问题是缓存没有及时清理,如果您使用
imageNamed:
分配大量图像,您的应用程序可能会崩溃。解决方案:
直接从
Bundle
分配图像:小问题:
Images.xcassets
中的图像从未分配。因此,将图像移到Images.xcassets
外部以直接从 Bundle 分配。There are at least 3 ways to animate an array of images through a
UIImageView
. I'm adding 3 links to download sample code for the 3 possibilities.The first one is the one that everyone knows. The other ones are less known.
- UIImageView.animationImages
Example Link
The problem of this one is that do not have Delegate to tell us in which moment the animation is finished. So, we can have problems if we want to display something after the animation.
In the same way, there is no possibility to kept the last image from the animation in the
UIImageView
automatically. If we combine both problems we can have a gap at the end of the animation if we want to kept the last frame on screen.- CAKeyframeAnimation
Example Link
This way to animate works through
CAAnimation
. It have an easy delegate to use and we can know when the animation finish.This is probably the best way to animate an array of images.
Delegate:
- CADisplayLink
Example Link
A CADisplayLink object is a timer object that allows your application to synchronize its drawing to the refresh rate of the display.
This way to do it is really interesting and opens a lot of possibilities to manipulate what are we showing in screen.
DisplayLink getter:
Methods:
GENERAL PROBLEM:
Even though we have this 3 possibilities, if your animation is with a lot of big images, consider using a video instead. The usage of memory will decrease a lot.
A General problem you will face doing this is in the moment of the allocation of the images.
If you use [UIImage imageNamed:@"imageName"] you will have cahe problems.
From Apple:
This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method locates and loads the image data from disk or asset catelog, and then returns the resulting object. You can not assume that this method is thread safe.
So,
imageNamed:
stores the image in a private Cache.- The first problem is that you can not take control of the cache size.
- The second problem is that the cache did not get cleaned in time and if you are allocating a lot of images with
imageNamed:
, your app, probably, will crash.SOLUTION:
Allocate images directly from
Bundle
:Small problem:
Images in
Images.xcassets
get never allocated. So, move your images outsideImages.xcassets
to allocate directly from Bundle.请参阅
UIImageView
。很难说它是否符合您的需求,因为您没有向我们提供详细信息,但这是一个良好的开始。See the
animationImages
property ofUIImageView
. It’s hard to say if it fits your needs as you don’t give us details, but it’s a good start.我为此添加了 swift 3.0 扩展
I have added a swift 3.0 extension for this
对我来说最好的解决方案是使用 CADisplayLink。 UIImageView 没有完成块,您无法捕捉动画步骤。在我的任务中,我必须逐步使用图像序列器更改视图背景。因此 CADisplayLink 允许您处理步骤并完成动画。如果我们谈论内存的使用,我认为最好的解决方案是从捆绑中加载图像并在完成
ImageSequencer.h
ImageSequencer.m后删除数组
Best solution for me use CADisplayLink. UIImageView doesn't have completion block and you can't catch steps of animation. In my task i must changing background of view with image sequencer step by step. So CADisplayLink allows you handling steps and finishing animation. If we talk about usage of memory, i think best solution load images from bundle and delete array after finishing
ImageSequencer.h
ImageSequencer.m
这是一个简单且有效的动画代码。/
This is a simple and working code for animation./