Ray Wenderlich Course - SwiftUI (Part 3)
Cracking on with SwiftUI! As I've said, this has been a tricky course but I'm learning plenty. Let's continue!
Start Time - 19:55
Slider and Stepper
Stepper - plus or minus a value (buttons for this)
Slider - as with bullseye
State - use of @State and the $ symbol.
Start Time - 19:55
Slider and Stepper
Stepper - plus or minus a value (buttons for this)
Slider - as with bullseye
State - use of @State and the $ symbol.
struct ContentView: View {
@ State private var orderCount = 0
var body: some View {
VStack {
Stepper(onIncrement: { self.orderCount += 1
} , onDecrement: { }) {
Text("Set order amount")
}
}
}
}
A bit about state - this is a value that can change and will update accordingly.
So this is a lot easier!
Stepper("Set order amount", value: $orderCount)
Slider(value: $houseTemperature, in: (30...120))
Text("\(houseTemperature)"))
A nice bit here for Stepper and Slider. I will experiment with those!
Date Picker and Toggle
Date Picker - storing the date. Depending what info you want!
So another use of the @State - it is about passing in a value that is binded, rather than a straight value into the object, in this case a toggle...
@State var isCatNipOn = false
var body: some View {
VStack {
Toggle("Activate cat nip", isOn: $isCatNipOn)
}
}
A couple of really good uses of the ternary operator, rather than an if statement! The below is also a computed property.
var isActivatedMessage: String {
return "Cat nip is " + (isCatNipOn ? "Activated!": "Deactivated!")
}
And again but with a text property - cool!
Text(isActivatedMessage)
.foregroundColor(isCatNipOn ? .green: .red)
So lots of cool stuff here! I've learned about toggle and date picker. Definitely want to try these out and explore more!
struct ContentView: View {
var isActivatedMessage: String {
return "Cat nip is " + (isCatNipOn ? "Activated!": "Deactivated!")
}
var dateFormatter: DateFormatter {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
return dateFormatter
}
@State var isCatNipOn = false
@State var pickedDate = Date()
var body: some View {
VStack {
Toggle("Activate cat nip", isOn: $isCatNipOn)
Text(isActivatedMessage)
.foregroundColor(isCatNipOn ? .green: .red)
.fontWeight(isCatNipOn ? .heavy: .regular)
Toggle(isOn: $isCatNipOn) {
VStack {
Image("CatNip")
.resizable()
.frame(width: 100, height: 100)
Text("Activate cat nip!")
}
}
DatePicker(selection: $pickedDate, displayedComponents: [.date]) {
Text("Select Date")
}
Text("\(dateFormatter.string(from: pickedDate))")
}
}
}
And there we go! Part 1 complete of this course. Three more parts to go, so I reckon at least another couple of weeks for this SwiftUI course. I'm learning plenty - the next challenge will be putting it all into practice.
Finish Time - 20:43 (48 minutes)
Summary -
- How to create different objects (slider, stepper, date picker and toggle)
- What @State means and why we need this and the $
- Use of ternary operator in context
- Using ternary to choose different properties
Comments
Post a Comment