- Personally, I get annoyed
4 days 1 hour ago - Nice!
1 week 3 days ago - I wouldn't want to type a
1 week 3 days ago - Since you say that you like
1 week 4 days ago - Thank you
1 week 5 days ago - Buggy colors
3 weeks 3 days ago - Yes, you can create a layer
3 weeks 5 days ago - UIImage for tiles?
3 weeks 6 days ago - I don't think I'll be joining you
5 weeks 5 days ago - Thanks... you saved me a days
6 weeks 1 day ago
iPhone Sample Code: Tiles
As an exercise in using the Core Animation API, I've implemented a little iPhone app that reproduces the behavior of the iPhone home screen's icon reorganization interface. (You know, dragging the wiggly icons around.) You can download my sample code to see how it works. Some descriptions of the highlights follow below.

The source code and Xcode project can be downloaded here: Tiles-v1.0.zip.
(If you're a Mercurial user, you may want to grab it from bitbucket.org.)
The primary classes to look at are TilesViewController and Tile. The view controller implements all of the "logic" of the application, while the Tile class has the animations.
An instance of Tile represents one of the icons, and is derived from CAGradientLayer. The gradient layer properties get set to provide a gloss effect for the tiles. Tile also provides a few animations, initiated by calling these methods:
appearDraggable: Changes the tile to be partially transparent, and makes it slightly bigger. This is invoked when the user touches a tile.appearNormal: Reverses the effects ofappearDraggable. This is invoked when the tile is released.startWiggling: Starts a tile "wiggling", as in the iPhone home screen while in reorganization mode.stopWiggling: Stops the wiggling effect
The TilesViewController class is pretty straightforward. When the user touches a the screen, the touchesBegan method determines which tile was touched, calls its appearDraggable method, and calls other tiles' startWiggling methods.
As the user drags the tile around the screen, the touchesMoved method moves the dragged tile, and moves the other tiles as needed to provide an open space for it. Core Animation takes care of all the zooming around of the icons.
When the user lets go of the tile, the touchesEnded method drops it in place and removes all the animations.
Things I learned from this project:
- Turning on the
masksToBoundsproperty for layers slows things down quite noticeably. - When hit-testing layers, you have to use a layer's presentation layer, not a model layer itself.
CAGradientLayeris easy to use.
Here are some things I don't understand. (Maybe some smart person can explain.)
- When hit-testing to see which layer was touched, I had to do both
[touch locationInView:view]and[view convertPoint:location toView:nil]. However, when handling touch-moves, I only have to use[touch locationInView:view]. I don't understand why the coordinate systems are (apparently) different.
Comments
UIImage for tiles?
This looks great and I'm going to start experimenting tonight. But perhaps you know the answer before I pull out too much hair. I'd like to modify this to display images (UIImage objects). From a quick look at the code I think I can replace the CAGradientLayers (and not do the gloss manipulation & drawing), but if you have any advice I'd appreciate it.
Yes, you can create a layer
Yes, you can create a layer and set the
contentsproperty to whatever image you want.