Swiftmas – Day 09 – Sparkles

Posted by

·

What do sparkles mean to you?

For the Ninth prompt, we have Sparkles. What will you create?

I’m looking forward to seeing what you come up with today.

Build a Christmas Lights Simulator in SwiftUI

Light up your holiday season with this Christmas Lights Simulator App! In this tutorial, we’ll create a fun app where animated “light bulbs” change colors dynamically. Users can control the animation and even randomize the light colors for added excitement.

Getting Started


1. Set Up Your Project

You can find the sample project in the GitHub repository here

Create a new SwiftUI project in Xcode:

  1. Open Xcode and select Create a new project.
  2. Choose the App template, click Next.
  3. Enter your project name (e.g., “ChristmasLightsSimulator”), set the interface to SwiftUI, and click Create.

What You’ll Learn

  1. Displaying multiple items using SwiftUI’s ForEach.
  2. Animating elements with a timer.
  3. Adding user controls for starting, pausing, and randomizing lights.
  4. Styling UI elements with gradients, shadows, and dynamic colors.

Step 1: Creating the Light Bulb Layout

The app’s main focus is on six light bulbs displayed horizontally. Let’s create this layout using an HStack and style each light bulb as a colored circle.

Code Snippet: Light Bulbs

Explanation:

  1. ForEach(0..<6): Creates six circles, one for each light bulb.
  2. Dynamic Colors:
  • activeColorIndex ensures the colors cycle through the lightColors array.
  • Each bulb’s color is determined by its index and the active color.
  1. Styling:
  • The Circle() is styled with a shadow for depth and a fixed size (50x50).

Step 2: Adding Animation

To animate the lights, we’ll use a Timer to update the activeColorIndex periodically.

Code Snippet: Animation Logic

Explanation:

  1. Timer:
  • Updates the activeColorIndex every 0.5 seconds.
  • Cycles through the lightColors array to change bulb colors.
  1. Toggle Animation:
  • isAnimating controls whether the animation is running.
  • Pauses animation by invalidating the timer when isAnimating is set to false.

Step 3: Adding Buttons for User Control

We’ll provide two buttons:

  1. A start/pause button to control the animation.
  2. A randomize button to shuffle the bulb colors.

Code Snippet: Control Buttons

Explanation:

  1. Start/Pause Button:
  • Toggles animation state with a label that updates based on isAnimating.
  • Changes color to red when paused or green when running.
  1. Randomize Button:
  • Calls randomizeColors, which shuffles the colors in lightColors.

Step 4: Shuffling the Light Colors

Let’s add a function to randomize the colors of the lights.

Code Snippet: Randomizing Colors

Explanation:

  • shuffled(): Randomizes the order of the colors in the lightColors array.
  • Dynamic Refresh: SwiftUI automatically refreshes the UI to reflect the updated color order.

Step 5: Styling the Background

A festive background enhances the holiday vibe. Let’s use a LinearGradient that blends dark and cool colors.

Code Snippet: Gradient Background

Explanation:

  1. Gradient:
  • Starts with black at the top and transitions to a soft blue at the bottom.
  • Provides a wintry, holiday-themed atmosphere.
  1. ignoresSafeArea: Ensures the gradient fills the entire screen.

Step 6: Bringing It All Together

Here’s the complete code for your Christmas Lights Simulator App:


Step 7: Run Your App

Run the app on an iOS Simulator or device. Test the following features:

  • Lights pause
  • Colours randomize
  • Lights animate

What’s Next?

Here are some ideas to make your app even more festive:

  1. Add More Colors: Include gradients or holiday-themed color combinations.
  2. Dynamic Speed Control: Add a slider to adjust the speed of the light transitions.
  3. Customizable Layout: Allow users to choose the number of light bulbs or their arrangement.

🎉 Congratulations! You’ve built a festive Christmas Lights Simulator App. 🎄


Discover more from

Subscribe to get the latest posts sent to your email.

thecodingsprite avatar

About the author

Hi! My name is Billie, my friends call me Billie Boo. I am a self taught iOS developer with a background in computer science, animation, graphic design & web design. I love sharing my knowledge & projects with the world & that is my mission for this blog. It’s never too late or too hard to follow your dreams.