In SwiftUI, one of the most important property wrappers is @State.
When we declare variables in Swift, they are meant to change. But by using @State we can change the values of these variables easily throughout the lifecycle of our app.
Hence these variables come to be called State Properties. They are easily alterable.
Let us take some code as an Example.
STEP 1
Declare a View called Example:
STEP 2
Now that our view is ready, we can declare a State property.
So,
@State var showText = false
STEP 3
Now we can apply the property. As the name of the property suggests we can use the variable to show and hide our text.
So lets add the conditions.
STEP 4
Now create a button that toggles the state property.
RESULT
Due to this code we get the following result!
See that was easy. Just like that we can change our views and use awesome techniques to enhance our UI and UX.
CHALLENGE:
Now try to make a SwiftUI View in which you use State Properties to make an animating shape. On tapping it, the color of the shape should change.
Do try it and share your creations with me on Twitter.
HAPPY CODING!