Member-only story
New in SwiftUI 4: Breathe UI with .onReceive Protocol & Timer.
Hello everyone, welcome to a new SwiftUI tutorial. I hope you are all doing well? Today I am going to show you how to use .onReceive Protocol & Timer in SwiftUI 4 and Xcode 14.
What is .onReceive in SwiftUI?
This has two steps in it’s parameters. publisher
The publisher to subscribe to.action
The action to perform when an event is emitted by publisher
. The event emitted by publisher is passed as a parameter to action
. Adds an action to perform when this view detects data emitted by the given publisher.
What is a Timer in SwiftUI?
Timers work in conjunction with run loops. Run loops maintain strong references to their timers, so you don’t have to maintain your own strong reference to a timer after you have added it to a run loop.To use a timer effectively, you should be aware of how run loops operate.
Let’s get started with opening up Xcode 14. We are going to start by adding in the assets for this project. All Assets are available in the code file at the bottom or use your own. 👍 Let’s open a new SwiftUI file called BreatheType.