Learning Swift – Day 205 to 207

Day 205

100 Days of SwiftUI – Day 19

This day was dedicated to creating our own app from scratch in the form of a Unit Converter. I have some plans on writing about that so I didn’t create a blog post about it yet. Stay tuned for news.

Day 206

100 Days of SwiftUI – Day 20

In this day we learned some more basics of SwiftUI, in particular how to use Stacks, Buttons, Alerts, Colors, Frames, Gradients and more.

You can find the project with all the explanations in comments at this link.

 100 Days of SwiftUI – Day 21

In this day we created a whole game from scratch, a guess-the-flag game that was super-fun to create! This is the same as Project 2 of Hacking with Swift but SwiftUI allowed for some very nice graphical tweaks to make the game much more beautiful than it would have been in UIKit with the same lines of code. Of course everything is possible but, I start to see why SwiftUI will be the future.

You can find the project in its current state at the same link posted above, just a few commits beyond.

Day 207

So difficult to have something done today. I worked four and a half NET hours on the current music notation project I am involved with and then had to clean so many things in the house. Freelancer life is wonderful, just sometimes very hard to stay on tracks with the things you have to do.

100 Days of SwiftUI – Day 22

Review

  1. ZStack lets us layer views on top of each other. The Z refers to Z index, which is how we refer to depth in 3D graphics
  2. Semantic colors are colors that are named according to their use rather than according to their hue (sfumatura in Italian). For example, color.primary might be light or dark depending on the device theme.
  3. Gradients can be created using an array of colors or an array of color stops. Color stops include both a color and a position inside the gradient. It is not yet totally clear to me how we create and use color stops and what they are in practice.
  4. Colors are views in SwiftUI. This allows us to use them directly inside stacks.
  5. The frame() modifier lets us specify a precise width and height for a view but we don’t need to specify both width and height if we don’t want to.
  6. SwiftUI let’s us draw shadows with a specific color and radius. If we use bright colours we can use this to create glow effects.
  7. The safe area excludes any space allocated to the home indicator as well as excluding the status bar.
  8. Buttons can be plain text, they can have images, or they can have other kinds of views. We can use multiple views by placing them inside a stack.
  9. Buttons must be given a closure to be run when they are tapped. Without some sort of action to run, the button would be pointless.
  10. The Int.random(in:) method chooses one random integer in a range we specify.. We can use closed ranges or half-open ranges, depending on what we need.
  11. We can embed a HStack inside a VStack. This lets us make grid structures fairly easily.
  12. We can draw borders with a custom shape by using the overlay() modifier. We can apply separate modifiers to that overlay, such as a border.

Challenges

Challenge 1: add an @State property to store the user’s score, modify it when they get an answer right or wrong, then display it in the alert.

  • First: add an @State private var score = 0 statement in the properties section of the file.
  • Second: in the flagTapped(_:) method, add a score += 1 to the if statement and a score -= 1 to the else part.
  • Third: inside the Alert() change the message to be Text("Your score is \(score)"

That’s it!

Challenge 2: show the player’s current score in a label directly below the flags.

  • I tried to have as much fun as possible here: I created a Text("Score: \(score)"and then added the following modifiers:
    • a .foregroundColor(.white) to make it stand out against the background.
    • a .font(.title) to make it big!
    • a .fontWeight(.heavy) to make it super bold but not as bold as the title
    • Finally, a .shadow(color: .yellow, radius: 4), which makes it sparkling!

The great thing here is how the label updates immediately without needing extra work! In UIKit, this was much harder, even if, for the brain, it was probably better! If we only get to know this, our brain will never work enough on things and when a real problem will hit … BUM! We will be on the floor!

Challenge 3: when someone chooses the wrong flag, tell them their mistake in your alert message — something like “Wrong! That’s the flag of France”, for example.

  • In the flagTapped(_:) method, in the else statement, change the scoreTitle to be "Wrong!\nThat's the flag of \(countries[number])!"

That’s it!

It is not 11.36pm on October 15th and, in a few minutes, it will be my birthday so I don’t want to stay up more! Tomorrow I will have the heaviest of days in teaching but I will try to see if the equivalent in the 100 Days of Swift had some more challenges to add!

Goodnight, world!


Thank you for reading!

If you like what I’m doing here please consider liking this article and sharing it with some of your peers. If you are feeling like being really awesome, please consider making a small donation to support my studies and my writing (please appreciate that I am not using advertisement on my articles).

If you are interested in my music publications don’t forget visit my Facebook page and the pages where I publish my scores (Gumroad, SheetMusicPlus e ScoreExchange).

You can also support me by buying Paul Hudson’s book from this Affiliate Link.

Anyways, thank you so much for reading!

Till the next one!

Published by Michele Galvagno

Professional Musical Scores Designer and Engraver Graduated Classical Musician (cello) and Teacher Tech Enthusiast and Apprentice iOS / macOS Developer Grafico di Partiture Musicali Professionista Musicista classico diplomato (violoncello) ed insegnante Appassionato di tecnologia ed apprendista Sviluppatore iOS / macOS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: