Open up the ViewController file and in the function buttonDidPressDown we are going to add the code for our animation and below that we will need one helper method. To understand how to use CAEmitterLayer you can view the documentation at https://developer.apple.com/reference/quartzcore/caemitterlayer. Quickly glancing through the docs you can see that the CAEmitterLayer needs and array of CAEmitterCells. The cells will be the particles that animate around and the cells are where we will add the animation properties. If you look through the docs on the CAEmitterCell you will see that they have quite a lot of animatable properties. Let’s define that helper function that we talked about that will be a factory method to create some cells.

This is really not as complex as it might look it you read through all the properties. The first three lines create the cell and set a small star to the cell contents. The rest of the lines create a birthrate(new cells per second), a lifetime, variance, velocity, spin, scale, color, and alpha. The range properties allows each to vary. To create the animation all we need to do is create the CAEmitterLayer and add that to the layer of the target, the button in our case. Go to buttonDidPressDown

So in the pressDown we create the CAEmitterLayer and set the position inside the UIButton to the center. We then set the size. We call our cell factory and get a cell. Then we add the CAEmitterLayer to the button’s sublayer. It will immediately start animating. The next bit of code simply removes the particleEmitter layer after 0.8 seconds have occurred. Run the project and enjoy.