Swiftmas – Day 08 – Festive Challenge

Posted by

·

Question is, how challenging is the code you’re going to create going to be?

For the Eighth prompt, we have Festive Challenge. What will you create?

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

Bring festive fun into your day with a Holiday Challenge App! In this step-by-step guide, we’ll create an app where users can generate random holiday-themed challenges like “Bake holiday cookies” or “Build a snowman.” It’s a perfect way to add holiday cheer to your SwiftUI project.


What You’ll Learn

  1. Displaying dynamic content using @State.
  2. Adding animations to make your app more engaging.
  3. Styling UI components like buttons and text for a festive look.
  4. Using RadialGradient to create a holiday-inspired background.

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., “HolidayChallengeApp”), set the interface to SwiftUI, and click Create.

Step 1: Setting Up the Challenge List

We need a list of holiday challenges that users can randomly select from. We’ll define these challenges as a state variable.

Code Snippet: Challenge List

Explanation:

  • The @State property wrapper lets SwiftUI monitor changes to the challenges array.
  • This array holds all the holiday-themed challenges that users can randomly select.

Step 2: Displaying the Challenge

We’ll use another @State variable, currentChallenge, to keep track of the selected challenge and display it to the user.

Code Snippet: Displaying the Current Challenge

Explanation:

  1. currentChallenge: Stores the current challenge to display.
  2. showChallenge: Controls whether the challenge text is visible.
  3. Animation: A smooth scaling and fading effect is applied when the challenge text appears.

Step 3: Generating a New Challenge

Let’s create a function to randomly select a challenge from the challenges array and show it on the screen.

Code Snippet: Generate a Random Challenge

Explanation:

  • randomElement(): Selects a random item from the challenges array.
  • withAnimation: Ensures the challenge appears with an animation when updated.

Step 4: Adding a Button

We’ll add a button labeled “New Challenge” to let users generate challenges on demand.

Code Snippet: Challenge Button

Explanation:

  • Button Styling:
    • A green background with rounded corners for a festive feel.
    • A shadow effect to make the button stand out.
  • Action: Calls generateChallenge when tapped.

Step 5: Festive Background

Let’s give our app a cheerful holiday look by adding a RadialGradient as the background.

Code Snippet: Festive Background

Explanation:

  • Radial Gradient: Creates a festive green-to-white gradient radiating from the center of the screen.
  • ignoresSafeArea: Ensures the background extends across the entire screen.

Step 6: Bringing It All Together

Here’s the complete code for your Holiday Challenge App:


Step 7: Run Your App

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

  • Animations work nicely
  • Challenges are random
  • Button works

What’s Next?

Here are some ideas to expand your Holiday Challenge App:

  1. Track Completed Challenges: Add a button to mark challenges as completed and show a list of completed tasks.
  2. Share Challenges: Let users share challenges with friends via social media or messaging.
  3. Customize Challenges: Allow users to add their own challenges to the list.

🎉 Congratulations! You’ve built a festive app to spread holiday joy.


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.