子类化 UIView 以创建动画 UIImageView 的最有效方法

发布于 2024-08-07 16:37:40 字数 401 浏览 2 评论 0原文

我需要播放基于帧的动画。我尝试了带有animationImages的UIImageView,但它没有给我对动画的任何控制。我无法暂停它,我无法屏蔽它,我无法知道当前帧等。

所以我对 UIView 进行了子类化并创建了一个名为 AnimationView 的东西,它获取图像数组并使用 NSTimer 执行动画。在drawRect:中,我可以做所有的遮罩和我想要的其他一切。

然而这种方法太慢了。我无法以 30 fps 运行动画,甚至可能无法以 10 fps 运行动画,而且我正在 3GS 上进行测试。 (我只在每一帧上进行遮罩和混合:) - 并且使用相同的图像在 UIImageView 上以 30fps 播放良好)。

那么,实现这一目标的最有效方法是什么? NSTimer 是一个糟糕的选择吗?有更好的方法吗?

谢谢 :)

I need to play frame based animations. I tried the UIImageView with animationImages but it doesn't give me any control over my animation. I can't pause it, I can't mask it, I can't know the current frame etc.

So I subclassed UIView and created something I called AnimationView which takes the array of images and does the animation using an NSTimer. In the drawRect: I can then do all the masking and everything else I want.

However this method is way too slow. I can't run an animation at 30fps, maybe not even at 10fps and I am testing on a 3GS. (I am only doing masking and blending on every frame :) - and using the same images plays fine at 30fps on a UIImageView).

So, what is the most efficient way to achieve this? Is the NSTimer a bad choice? Is there a better way around it?

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玻璃人 2024-08-14 16:37:40

In this answer to this question Mo DeJong provides a link to a source code implementation of a class that manually animates PNG image frames. He claims to get 15 FPS for 480x320 images animating on a non-3GS iPhone.

夏末染殇 2024-08-14 16:37:40

NSTimer 很好。问题在于,您使用 Quartz(drawRect:)在每一帧中使用 CPU 触摸大量像素。如果您必须使用遮罩合成图像,您想要做的是使用 OpenGL,或者将图像缓存在 CALayers 或 CGImages 中。

The NSTimer is fine. The problem is that you're touching a lot of pixels using the CPU in every frame using Quartz (the drawRect:). What you want to do is either use OpenGL if you have to compose images using a mask or cache your images in CALayers or CGImages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文