Start 100DaysOfSwiftUI from 2020.Mar.18th.
Day 20, 21, 22: Project 2, part one, two, three
done with 1 hour
New findings:
We can place Color.red in ZStack.
modifier “edgesIgnoringSafeArea” will expand to safe area
modifier “renderingMode” also will be useful
modifier “overlay” is much interested to make attractive screen
Question?
Who will maintain showingAlert variable after alert appeared? I guess someone maintain the variable to make alert disappeared?
-> no need to maintain. Probably Alert window maintain the variable which is passed via isPresented. (it is reasonable/understandable design)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
// // ContentView.swift // ProjectTwo // // Created by Tomoaki Yagishita on 2020/03/29. // Copyright © 2020 SmallDeskSoftware. All rights reserved. // import SwiftUI struct ContentView: View { @State var countries = ["Estonia", "France", "Germany", "Ireland", "Italy", "Nigeria", "Poland", "Russia", "Spain", "UK", "US"].shuffled() @State var correctAnswer = Int.random(in: 0...2) @State private var showingScore = false @State private var scoreTitle = "" @State private var userScore = 0 var body: some View { ZStack { LinearGradient(gradient: Gradient(colors: [.blue, .black]), startPoint: .top, endPoint: .bottom) .edgesIgnoringSafeArea(.all) VStack(spacing:30) { VStack { Text("Tap the flag of") .foregroundColor(.white) Text(countries[correctAnswer]) .font(.largeTitle) .fontWeight(.black) .foregroundColor(.white) } ForEach(0 ..< 3) { number in Button(action: { self.flagTapped(number) }) { Image(self.countries[number]) .renderingMode(.original) .clipShape(Capsule()) .overlay(Capsule().stroke(Color.black, lineWidth: 1)) .shadow(color: .black, radius: 2) } } Text("Current score : \(userScore)").foregroundColor(.white) Spacer() } } .alert(isPresented: $showingScore) { Alert(title: Text(scoreTitle), message: Text("Your score is \(userScore)"), dismissButton: .default(Text("Continue")) { self.askQuestion() }) } } func flagTapped(_ number:Int){ if number == correctAnswer { scoreTitle = "Correct" userScore += 3 } else { scoreTitle = "Wrong! \nThat is the flag of \(countries[number])." userScore -= 1 } showingScore = true } func askQuestion() { countries.shuffle() correctAnswer = Int.random(in: 0...2) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } |
Sponsor Link