Start 100DaysOfSwiftUI from 2020.Mar.18th.
Day 35: Milestone: Projects 1-3
done 25min + 30min
New findings: followings are new findings : none because it is challenge day
Code for final(?) version
-
Features
- if answer is wrong, text will be changed to red
- no way to restart….
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 79 80 81 82 |
struct ContentView: View { @State private var underSetting = true @State private var whichSeries:Int = 1 @State private var howManyQuestions:Int = 5 // for play private var baseListForQuestions:[Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9] @State private var listForQuestions:[Int] = [] var body: some View { Group { if underSetting { SettingView(underSetting: $underSetting, whichSeries: $whichSeries, howManyQuestions: $howManyQuestions) } else { PlayView(series: $whichSeries, listForQuestions: $listForQuestions) .onAppear(perform: makeQuestions) } } } func makeQuestions() { listForQuestions = [] for _ in 1...howManyQuestions { let newQuiz = Int.random(in: 1...8) listForQuestions.append(baseListForQuestions[newQuiz]) } return } } struct PlayView: View { @Binding var series:Int @Binding var listForQuestions:[Int] @State var answeres:[String] = ["", "", "", "", "", "", "", "", "", ""] @State var correctFlags:[Bool] = [true, true, true, true, true, true, true, true, true, true] var body: some View { VStack { Text("Playing") ForEach(0..<listForQuestions.count, id: \.self) { index in HStack { Text("\(self.series) x \(self.listForQuestions[index]) = ") TextField("", text: self.$answeres[index], onCommit: { self.checkAnswer(index: index) }) .foregroundColor(self.correctFlags[index] ? Color.black : Color.red) .textFieldStyle(RoundedBorderTextFieldStyle()) .labelsHidden() } .padding() } } } func checkAnswer(index:Int) { let correctAnswer = series * listForQuestions[index] correctFlags[index] = Int(answeres[index]) == correctAnswer return } } struct SettingView: View { @Binding var underSetting: Bool @Binding var whichSeries:Int @Binding var howManyQuestions:Int var body: some View { Form { Text("Setting") Stepper("which series \(whichSeries)", value: $whichSeries, in: 1...9) Stepper("how many questions : \(howManyQuestions)", value: $howManyQuestions, in: 1...10) Button(action: { self.underSetting = false }) { Text("setting done") } } } } |
code for setting view (very basic version)
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 |
struct ContentView: View { @State private var underSetting = true @State private var whichSeries:Int = 1 @State private var howManyQuestions:Int = 5 var body: some View { Group { if underSetting { SettingView(underSetting: $underSetting, whichSeries: $whichSeries, howManyQuestions: $howManyQuestions) } else { PlayView() } } } } struct PlayView: View { var body: some View { Text("Playing") } } struct SettingView: View { @Binding var underSetting: Bool @Binding var whichSeries:Int @Binding var howManyQuestions:Int var body: some View { Form { Text("Setting") Stepper("which series \(whichSeries)", value: $whichSeries, in: 1...9) Stepper("how many questions : \(howManyQuestions)", value: $howManyQuestions, in: 1...10) Button(action: { self.underSetting = false }) { Text("setting done") } } } } |
Sponsor Link