UIKit Particle Systems in iOS 5 Tutorial

Update 10/24/12: If you’d like a new version of this tutorial fully updated for iOS 6 and Xcode 4.5, check out iOS 5 by Tutorials Second Edition! Note from Ray: This is the fifteenth and final iOS 5 tutorial in the iOS 5 Feast! This tutorial is a free preview chapter from our new book […] By Ray Wenderlich.

Leave a rating/review
Save for later

Let's end this feast with a tasty snack - UIKit Particle Systems in iOS 5!

Update 10/24/12: If you’d like a new version of this tutorial fully updated for iOS 6 and Xcode 4.5, check out iOS 5 by Tutorials Second Edition!

Note from Ray: This is the fifteenth and final iOS 5 tutorial in the iOS 5 Feast! This tutorial is a free preview chapter from our new book iOS 5 By Tutorials. This Wednesday we’ll have our final post in the iOS 5 Feast series – the epic iOS 5 Feast Giveaway – so last chance for #ios5feast tweets! :]

This is a blog post by iOS Tutorial Team member Marin Todorov, a software developer with 12+ years of experience, an independant iOS developer and the creator of Touch Code Magazine.

You’ve probably seen particle systems used in many different iOS apps and games for explosions, fire effects, rain or snow falling, and more. However, you probably saw these types of effects most often in games, because UIKit didn’t provide a built-in capability to create particle systems – until iOS 5, that is!

Now with iOS 5, you can use particle systems directly in UIKit to bring a lot of exciting new eye-candy to your apps. Here are a few examples of where particle systems could be useful:

  • UIKit games: Yes, you can make games with plain UIKit (and some types of games work really well there, particularly card games and the like). But now, you can make them even better with explosions, smoke, and other goodies!
  • Slick UI effects: When your user moves around an object on the screen it can leave a trail of smoke, why not?
  • Stunning screen transitions: How about presenting the next screen in your app while the previous one disappears in a ball of fire?

Hopefully you’ve got some cool ideas of what you might want to use UIKit particle systems for. So let’s go ahead and try it out!

In this tutorial, we’re going to develop an app called “Draw with fire” that lets you (you guessed it) draw with fire on the screen.

I’ll take you along the process of creating the particle systems and having everything set on the screen, and you can develop the idea further into your own eye-candied drawing application. When the app is ready, you’ll be able to use it to draw a nice question mark of fire – like this one:

UIKit Particle Systems in iOS 5 Example

The New Particle APIs

The two classes you will need to use in order to create particle systems are located in the QuartzCore framework and are called CAEmitterLayer and CAEmitterCell.

The general idea is that you create a CAEmitterLayer, and add to it one or more CAEmitterCells. Each cell will then produce particles in the way it’s configured.

Also, since CAEmitterLayer inherits from CALayer, so you can easily inject it anywhere in your UIKit hierarchy!

I think the coolest thing about the new UIKit particle systems is that a single CAEmitterLayer can hold many CAEmitterCells. This allows you to achieve some really complicated and cool effects. For example, if you’re creating a fountain you can have one cell emitting the water and another emitting the vapor particles above the fountain!

Getting Started

Fire up Xcode (no pun intended) and from the main menu choose File\New\New Project. Select the iOS\Application\Single View Application template and click Next. Enter DrawWithFire for the product name, enter DWF for the class prefix, select iPhone for the Device Family, and make sure that “Use automatic reference counting” is checked (leave the other checkboxes unchecked). Click Next and save the project by clicking Create.

Select your project and select the DrawWithFire target. Open the Build Phases tab and open the Link Binary With Libraries fold. Click the plus button and double click QuartzCore.framework to add Quartz drawing capabilities to the project.

We’ll start the project by creating a custom UIView class which will have CAEmitterLayer as its layer. You can actually achieve this very very easy by overwriting the +(Class)layerClass method of the UIView class and returning a CAEmitter class. Pretty cool!

Create a new file with the iOS\Cocoa Touch\Objective-C class template, name the class DWFParticleView, and make it a subclass of UIView.

Open DWFParticleView.m and replace it with the following:

#import "DWFParticleView.h"
#import <QuartzCore/QuartzCore.h>

@implementation DWFParticleView
    CAEmitterLayer* fireEmitter; //1

    //set ref to the layer
    fireEmitter = (CAEmitterLayer*)self.layer; //2

+ (Class) layerClass //3
    //configure the UIView to have emitter layer
    return [CAEmitterLayer class];


Let’s go over the initial code:

  • We create a single private instance variable to hold our CAEmitterLayer.
  • In awakeFromNib we set fireEmitter to be the view’s self.layer. We store it in the fireEmitter instance variable we created, because we’re going to set a lot of parameters on this later on.
  • +(Class)layerClass is the UIView method which tells UIKit which class to use for the root CALayer of the view. For more information on CALayers, check out the Introduction to CALayer Tutorial.

Next let’s our view controller’s root view to DWFParticleView. Open up DWFViewController.xib and perform the following steps:

Setting a custom class in the Identity Inspector in Interface Builder

  1. Make sure the Utilities bar is visible (the highlighted button on the image above should be pressed down).
  2. Select the gray area in the Interface builder – this is the view controller’s root view.
  3. Click the Identity Inspector tab
  4. In the Custom class panel enter DWFParticleView in the text field.

At this point we have the UI all set – good job! Let’s add some particles to the picture.

A Particle Examined

In order to emit fire, smoke, waterfalls and whatnot you’ll need a good PNG file to start with for your particles. You can make it yourself in any image editor program; have a look at the one I did for this tutorial (it’s zoomed in and on a dark background so you can actually see the shape):

An example particle

My particle file is 32×32 pixels in size, it’s a transparent PNG file and I just used a little bit funkier brush to draw randomly with white color. For particles is best to use white color as the particle emitter will take care to tint the provided image in the colors we’d like to have. It’s also good idea to make particle image semi-transparent as the particle system can blend particles together by itself (you can figure out how it works by just trying out few different image files).

So, you can create a particle of your own or just use the one I made, but just be sure to add it to your Xcode project and have it named Particles_fire.png.


Over 300 content creators. Join our team.