0

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.

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?

Thanks.

domlao
  • 15,663
  • 34
  • 95
  • 134

2 Answers2

3

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.

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.

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

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.

Good luck.

Edit: 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)

Tom Duckering
  • 2,727
  • 1
  • 23
  • 27
  • Honestly, I doubt that he'll see a significant performance increase by using CALayers instead of UIViews. I've benchmarked both, and UIViews are only a hair slower to render and animate than CALayers. UIViews really are just a lightweight wrapper around CALayers. – Brad Larson Dec 05 '09 at 03:56
  • That said, if you want to see a CALayer-based touch handling example, you can check out the Core Plot framework, which uses only CALayers for its various rendering elements: http://code.google.com/p/core-plot/ . We've had touch-handling code in there for a while, but only recently have we started to use it. – Brad Larson Dec 05 '09 at 03:59
1

On Cocoa desktop, there's NSCell, which is a lightweight alternative to NSButton, but that's an older technology before CALayer and Core Animation (they don't conflicts with each other)

To make custom button, create a custom class either extends to delegates CALayer. Then to know which button is clicked, use [hitTest] method which will return the layer being clicked efficiently.

ivanTheTerrible
  • 2,836
  • 4
  • 25
  • 25