Created
October 24, 2024 00:12
-
-
Save takoikatakotako/71952bd91b6be0f8fa84882d4b3e578d to your computer and use it in GitHub Desktop.
SwiftUIでBMIを計算し、結果を別のViewで表示する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| struct ContentView: View { | |
| @State var height: String = "" | |
| @State var weight: String = "" | |
| @State var bmi: Double = 0 | |
| @State var showingSheet = false | |
| var body: some View { | |
| VStack { | |
| VStack(alignment: .leading) { | |
| Text("Height") | |
| TextField("Input Your Height", text: $height) | |
| .keyboardType(.numberPad) | |
| .padding() | |
| .border(Color.black, width: 1) | |
| } | |
| .padding() | |
| VStack(alignment: .leading) { | |
| Text("Height") | |
| TextField("Input Your Weight", text: $weight) | |
| .keyboardType(.numberPad) | |
| .padding() | |
| .border(Color.black, width: 1) | |
| } | |
| .padding() | |
| Button(action: { | |
| guard let height = Double(self.height), | |
| let weight = Double(self.weight) else { | |
| print("Fail to Calc BMI") | |
| return | |
| } | |
| // Calc BMI | |
| bmi = weight / (height / 100) / (height / 100) | |
| // Show Sheet | |
| showingSheet = true | |
| }, label: { | |
| Text("Calc BMI") | |
| .font(.title) | |
| .foregroundColor(Color.black) | |
| .padding() | |
| .background(Color(UIColor.lightGray)) | |
| .cornerRadius(16) | |
| }) | |
| } | |
| .sheet(isPresented: $showingSheet) { | |
| ResultView(bmi: $bmi) | |
| } | |
| } | |
| } | |
| #Preview { | |
| ContentView() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| struct ResultView: View { | |
| @Binding var bmi: Double | |
| var body: some View { | |
| VStack { | |
| Text("BMI: \(bmi)") | |
| if bmi < 18.5 { | |
| Text("You are thin") | |
| Image(.bellsprout) | |
| .resizable() | |
| .frame(width: 200, height: 200) | |
| } else if bmi > 25 { | |
| Text("You are fat") | |
| Image(.snorlax) | |
| .resizable() | |
| .frame(width: 200, height: 200) | |
| } else { | |
| Text("You are healthy") | |
| Image(.pikachu) | |
| .resizable() | |
| .frame(width: 200, height: 200) | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment