用什么代替UIButton来提高帧率

用什么代替UIButton来提高帧率

本文介绍了用什么代替UIButton来提高帧率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个扫雷克隆游戏,并制作了30 x 30的网格单元.现在,在我的初始化设计中,我计划为每个单元格使用UIButton,以便可以使用其touchDown和touchUpInside.但是我的问题是使用UIButton会使游戏缩放变慢,尤其是在UIView中加载或添加每个单元(900个)时.

I created a minesweeper clone game and I made a 30 x 30 grid of cells. Now in my initialize design, I planned to use UIButton for each cell so that I can use its touchDown and touchUpInside. But my problem is using UIButton makes the game slow in zooming and especially in loading or adding each cell(900 pcs) in a UIView.

现在,我计划使用图像代替UIButton,并且听说过CALayer可以简化动画.您能建议我如何使用此CALayer来实现或替换UIButton?

Now I'm planning to use images instead of UIButton and I heard about CALayer for easy animation. Can you suggest how can I use this CALayer for my implementation or replacement of UIButton?

谢谢.

推荐答案

我当然会建议您选择核心动画"路线.您可以使用NSView子类来接收和处理触摸事件,然后可以使用触摸的位置来计算被击中的单元格并适当地更新模型.

I'd certainly suggest going down the Core Animation route for this. You can use an NSView subclass to recieve and handle the touch events then you could then use the location of the touch to calculate the cell that's been hit and update your model appropriately.

您将CALayer安排在一个层次结构中-虽然视图树的复杂性将取决于您希望拥有的其他UI元素-您将拥有一个包含900个子层的CALayer.

You arrange CALayers in a heirarchy so - whilst the complexity of the tree of views will depend on other UI elements you wish to have - you'll have a CALayer containing the 900 sub layers.

使用Core Animation还可以让您对动画感到头昏眼花.

Using Core Animation will also allow you to get wizzy with animations.

这将变得更加复杂,但是您只需要硬着头皮,陷在Core Animation文档中和/或购买一本书.

It's going to be a bit more complex, but you just need to bite the bullet and get stuck into the Core Animation documentation and/or buy a book.

祝你好运.

Ivan建议使用hitTest消息.这样,您就可以让图层告诉您哪个图层被击中了.使用它显然是相当不错的.但是,如果您正在寻找速度,则可以更快地避免这种情况,然后解决它.这确实会假设您的游戏如何运作(即按钮/单元格不会移动位置).如果有机会,请尝试两种方式,并让我们知道如何实现.:o)

Ivan suggests using the hitTest message. This way you can get the layers to tell you which one of them was hit. Using that is obviously quite nice. However if you're looking for speed it might be quicker to avoid that and just work it out. This does make assumptions about how your game works (i.e. the buttons/cells don't move location). If you get the chance try both and let us know how it works out. :o)

这篇关于用什么代替UIButton来提高帧率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 10:28