So this is what we have so far and it’s pretty much a long way before we get to the final result which is this one. As you can see, the rotation is not that interesting and the pattern is not that interesting. The colors are using Stroke by default. Now, the question is how do we get there and also how do we use the best technique?
When I created this, I never anticipated how abstract this would be, meaning that the more abstract of an app that you’re trying to create, the less likely that you’re going to be able to reproduce the results a second time. And that’s exactly what happened to me. I’m trying to recreate it a second time and I run into an issue. Let’s take a look at the issue.
Right now, we have fixed the main error. I went into the next step and keep in mind that I created this the first time that I used SwiftUI and I didn’t know how to use it properly. The commands that I used at the time were not exactly as specific as they should be. So, for example, the next one, I was talking about turning a rectangle into small circles into a beautiful pattern. So beautiful pattern in itself is very vague. It doesn’t really tell the AI exactly what it should look like. And here, I’m saying that it should switch to another beautiful pattern, again very vague. So definitely avoid this kind of mistake.
What happened is that I was not able to reproduce the same results as the first time. And not just that, but at some point when I copy and paste the code that are changing, the result is not the same at all. In fact, it simply showed me a blank screen, meaning that it made a mistake in the way that it shows the pattern. It adds opacity and all that stuff, and despite my best efforts to keep telling it that I’m still seeing nothing over and over, it couldn’t fix itself. And this can happen, especially when you’re following my course. When you’re giving the same command and because it’s very vague, it’s very possible that you’re going to run into a problem where it doesn’t show what I’m showing here in my video.
So how do we deal with that and how do we go from here so that we can be more successful at the way that we can be consistent? Now, the first thing we’re going to do is to be a little bit more specific. So, as I mentioned before, what you should do, especially following this course, is that at the end of every section, I give you the final code. And that final code, you can essentially copy and paste the entire thing and give the next command. So in this case, I have the final code and I’m asking it to create a sunflower pattern. And I also want to try keeping it in one file. This is a good option if, for example, you’re prototyping and you don’t have a lot of code. Also, if you’re a beginner and you want to keep things simple, so keeping everything in one file is a good starting point.
Now, so far, I’ve showed you how to work with different files. This is definitely the best practice for a production-ready app. But, like I said, we are starting and it doesn’t hurt to do everything in a single file. So, what’s going to happen here is that it’s going to give me all the code in a single file. And typically, the single file is the main file. So, for each platform, you have different main files. So, for example, if you’re doing HTML, it’s going to be index.html. If you’re doing Python, it’s going to be main.py. If you’re doing React, it could be app.js or index.js. So, again, every platform has their main file. In the case of SwiftUI, it’s ContentView. So, we’re going to copy this code and paste that into the ContentView file.
Now, make sure that whatever you have in terms of the elements, because in SwiftUI, you don’t have to import anything, it just takes every single file that you have in your program. In this case, it just automatically knows that you have HologramShape, HologramView, and HologramEffect. So, if I put another HologramView here, it’s going to complain that it already exists. And that is different from other platforms, and it’s quite useful because then you don’t have to always import from HologramShape, from HologramView, which pretty much every other platform requires you to do. But on the opposite side, you have to make sure that you’re not repeating the same things. So, in this case, I’m going to paste the code to replace the code in ContentView. And now I have nothing that is repeating, thankfully.
Now, we can move on to the next step, which is to remove the yellow color and use a fill color for the circles instead of using a stroke. We’re also going to set the background to black so that the colors pop out a little bit more. So, here’s the updated code:
// Remove the yellow color
.fill(Color.yellow)
// Use a fill color for the circles
.stroke(Color.black, lineWidth: 1)
// Set the background to black
.background(Color.black)
Now, we have a beautiful pattern with randomized colors inspired by Apple’s colors. The circles are gradually bigger in the center and smaller outside. The background is black to make the colors pop out. This code change gives us the desired result.
In the next section, we will learn how to create the second pattern, add animation on tap, and handle code changes. Stay tuned!