Snow Animation with Swift 

First of all Happy New Year to everyone

In this article, I will show you how to make snow animation on the application screen.With the approach of the new year, companies use the new year themes as designs in their applications. In accordance with this concept, we will see snowflakes flying on our screen.

Having special developments, articles or photos for the relevant periods (holiday, feast, new year, etc.) in our application increases the interaction of the user with our application.

While doing this animation, I used CAEmitterCell and CAEmitterLayer classes. There are Apple’s documentation resource below for more detailed documentation.

Let’s Make It Snow

Into ViewController, we make definitions.

let snowFlakeLayer = CAEmitterLayer()
let snowFlakeItem = CAEmitterCell()

We set the properties of the SnowFlake Item that we defined in the next step.

snowFlakeItem.contents = UIImage (named: “snowFlake”) ?. cgImage →The image we added to our application
snowFlakeItem.scale = 0.10 → Size of each snowflake
snowFlakeItem.scaleRange = 0.3 → size of each snowflake
snowFlakeItem.emissionRange = .pi
snowFlakeItem.lifetime = 30 → Determines the length of time each snowflake will remain on the screen
snowFlakeItem.birthRate = 40 →Determines how many snowflakes to create per second. We can say the frequency of snowfall.
snowFlakeItem.velocity = -50 → Cell initial speed
snowFlakeItem.velocityRange = -40 → The speed of the cell will change by a random amount within the range specified by velocityRange.
snowFlakeItem.yAcceleration = 30
snowFlakeItem.xAcceleration = 0
snowFlakeItem.spin = -0.5 → rotation speed of snowflakes
snowFlakeItem.spinRange = 1.0 → Range of rotational speed of snowflakes

After completing the features that each snowflake can take, we will add these snowflakes into the layer.

snowFlakeLayer.emitterPosition = CGPoint (x: view.bounds.width / 2.0, y: -50)
snowFlakeLayer.emitterSize = CGSize (width: view.bounds.width, height: 0)
snowFlakeLayer.emitterShape = CAEmitterLayerEmitterShape.line
snowFlakeLayer.beginTime = CACurrentMediaTime ()
snowFlakeLayer.timeOffset = 10
snowFlakeLayer.emitterCells = [snowFlakeItem]

Finally, we add the layer to screen which we prepared with addSubview.


We made it;

Documentation Resource: CAEmitterCell and CAEmitterLayer


IOS Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store