基于UIView的动画 [英] UIView based animation

查看:119
本文介绍了基于UIView的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动画一些2D游戏的UIImageViews。使用[UiView开始/提交动画]是没有问题的,因为我需要始终确定所有图像视图的位置。相反,我改变了frame.origin.x / y在我的UIImageViews手动控制我的动画。我的问题是,帧大量下降显着为大量的UIImageViews或如果他们的大小太大(btw,UIImageViews包含PNGs与透明色)。您有任何优化建议吗?

I'm trying to animate some UIImageViews for a 2D game. Using [UiView begin/commit animation] is out of the question, since I need to determine the positions of all Image Views at all times. Instead I'm changing the frame.origin.x/y in my UIImageViews to manualy control my animations. My problem is that the framerate drops significantly for high numbers of UIImageViews or if their size is too large (btw, the UIImageViews contain PNGs with transparency colours). Do you have any optimization advices?

感谢

推荐答案

UIImageView只是为了动画显示(UIViews是CALayers的轻量级包装,它只是存储在显卡上的纹理),但你需要使用begin / commit动画块来获得平滑的动画。使用它,您可以通过访问UIView的背景层并获取其表示层来检索视图的当前位置。 Dustin Bachrach拥有帖子 a>描述了如何做到这一点。基本上,表示层是Core Animation最接近于实际图层在任何给定时间的位置,因此它的帧是在该时刻的UIView的帧。

UIImageView is just fine for animating about the display (UIViews are lightweight wrappers around CALayers, which are just textures stored on the graphics card), but you do need to use the begin / commit animation block to get smooth animation. Using that, you can retrieve the current position of the view by accessing the backing layer of the UIView and obtaining its presentation layer. Dustin Bachrach has a post that describes just how to do this. Basically, the presentation layer is Core Animation's closest approximation to where the actual layer is at any given time, so its frame is the frame of your UIView at that instant.

I 'm不确定触摸事件会找到他们的方式到视图,因为它在屏幕上移动,所以如果你需要让UIImageView响应触摸你可能需要替换它与CALayer,插入它作为主层的子层UIView的图层,并调用该主层的presentationLayer上的hitTest方法。这应该返回你的移动层的presentationLayer,如果它被点击,或者背景层,如果不是。

I'm not sure that touch events will find their way to the view as it's moving across the screen, so if you need to have the UIImageView respond to touches you may need to replace it with a CALayer, insert it as a sublayer of your main UIView's layer, and call the hitTest method on the presentationLayer of that main layer. That should return the presentationLayer of your moving layer if it was clicked on, or the background layer if not.

老实说,我不会去OpenGL,除非你绝对有为了性能原因,因为它是一个很难编码比Core Animation为您提供UIViews和CALayers。我已经能够使用UIViews和CALayers在60 FPS(100在30 FPS)动画50移动半透明层。

Honestly, I wouldn't go with OpenGL unless you absolutely had to for performance reasons, because it is a lot harder to code for than what Core Animation provides you with UIViews and CALayers. I've been able to animate 50 moving translucent layers at 60 FPS (100 at 30 FPS) using just UIViews and CALayers.

这篇关于基于UIView的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆