Search code examples
objective-ciosuiviewcore-animationuitouch

Creating gentle random movement with UIButtons and CoreAnimation


I'm trying to create an animation on the iPhone with a collection of UIButtons, each of which should slowly and randomly move around inside a UIView, bouncing off one another. I realise that this is a very open-ended question, so I'm just looking for a better name for such an effect, so that I can do some searching. Pulsating seemed logical, but most searches pull up regular movement in/out as a result for that.

I'm thinking almost like an "under a microscope" view of cells, where they're individually all moving around of their own accord, and bouncing off when they hit one another, or the effect of putting a lot of marbles in a box. I'll eventually want to implement the user "flicking" them about, but that's to come later.

Any ideas for where to begin looking? Thanks in advance!


Solution

  • I'd look into some 2D physics engines like Chipmunk or Box2D since they will provide collision detection and proper reactions on their own. You can then model your UIButtons as simple rigid bodies with very low initial velocity. Animating the buttons is as simple as manually moving them on every draw tick.