Swiftmas – Day 11 – Hot Drinks

Posted by

·

Who doesn’t love holding a hot cup of something at this time of year?

Happy Swiftmas Eve 😉

For the Eleventh prompt, we have Hot Drinks. What will you create?

I’m looking forward to seeing what you come up with today. Knowing how busy this time of year is, for the last two days I’m going to include the tutorial with the prompt…so spoiler alert…don’t scroll past this point if you don’t want the tutorial & sample project just yet!



Build a Hot Drink Customizer in SwiftUI

Happy Eve of Swiftmas End! We are going a bit deeper within our code today before a fun last day tomorrow.

Warm up your day with a Hot Drink Customizer App! This app guides users through selecting a hot drink, adding extras, and customizing their experience. It’s a great way to learn how to build an interactive SwiftUI app with dynamic views and state management.


What You’ll Learn

  1. Building a multi-step app flow using @State.
  2. Displaying content dynamically based on user actions.
  3. Reusing UI components with helper functions.
  4. Styling buttons and images for a polished user experience.

Getting Started


1. Set Up Your Project

You can find the sample project in the GitHub repository here

For this project I personally created an illustration of a drink. The layers have all been sized and placed for easy layering. You can download them below.

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

Step 1: Define the App State

To manage the user’s choices (e.g., drink type and extras), we’ll use @State variables. These variables will also help track the app’s current step in the flow.

Code Snippet: State Variables

Explanation:

  1. Drink Selections: Boolean variables (hasTea, hasCoffee, hasChocolate) track the user’s drink choice.
  2. Extras: Additional selections (hasMarshmallows, hasWhippedCream) for customizing the drink.
  3. questionTrack: Keeps track of the current step in the flow.

Step 2: Create a Multi-Step Flow

We’ll use a switch statement on questionTrack to control the displayed content. Each value of questionTrack corresponds to a specific step in the app.

Code Snippet: Multi-Step Flow

Explanation:

  • Each step has its own UI to keep things clean, there are many ways to achieve the same output so feel free to try building the end result your way:
  1. Yes/No Buttons (Step 0): Asks if the user wants a hot drink.
  2. Drink Buttons (Step 1): Lets the user select their drink.
  3. Extra Buttons (Step 2): Allows customization with extras.
  4. Enjoy View (Step 3): Displays the final message and a reset option.
  5. Goodbye View (Step 4): Shows a farewell message if the user declines.

Step 3: Adding Yes/No Buttons

The first step asks the user if they want a hot drink.

Code Snippet: Yes/No Buttons

Explanation:

  • Yes Button: Advances the flow to drink selection (questionTrack = 1).
  • No Button: Skips to the goodbye message (questionTrack = 4).

Step 4: Adding Drink Selection Buttons

Let the user choose between tea, coffee, or hot cocoa.

Code Snippet: Drink Buttons

Code Snippet: Reusable Drink Button

Explanation:

  1. Reusable Button: The drinkButton function generates a button for each drink option.
  2. Selection Handling:
  • Toggles the corresponding @State variable.
  • Advances the flow to the next step (questionTrack += 1).

Step 5: Adding Extras

The next step lets users customize their drink with marshmallows and whipped cream.

Code Snippet: Extra Buttons

Explanation:

  • Similar to the drink buttons, extras are handled with reusable buttons.

Step 6: Displaying the Final View

After customization, show the user a message and an option to restart.

Code Snippet: Enjoy View

Explanation:

  • Message: Displays “Enjoy!” to indicate the end of the flow.
  • Reset Button: Calls reset() to start over.

Step 7: Displaying a Goodbye Message

If the user declines a drink, display a farewell message.

Code Snippet: Goodbye View


Step 8: Visualizing the Cup

We’ll use a ZStack to layer images and display the user’s drink and extras.

Code Snippet: Cup View

Explanation:

  • Dynamic Images: Displays images for the selected drink and extras.
  • Layering: Stacks multiple images (e.g., steam, drink, cup) for a complete visual.

Step 9: Resetting the App

Add a function to reset all selections and restart the flow.

Code Snippet: Reset Function


Step 10: Adding Background and Styling

Let’s make the app visually appealing with a simple color background.

Code Snippet: Background Styling


Complete Code

Combine all the steps to create the final Hot Drink Customizer App.


Step 11: Run Your App

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

  • You can restart the app at the end of the drink flow.
  • You can restart the flow if you say no to a drink
  • View the drink layers as they should be.

What’s Next?

Here are some ideas to expand the app:

  1. Add More Extras: Include options like cinnamon, honey, or sugar cubes.
  2. Animate the Cup: Add animations to the drink preparation process.
  3. Save Preferences: Allow users to save their favorite combinations.

🎉 Congratulations! You’ve built a dynamic and interactive Hot Drink Customizer App. Here’s to the last day of #The12DaysOfSwiftmas tomorrow! ☕


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.