Member-only story

New in SwiftUI 4: Breathe UI with .onReceive Protocol & Timer.

AnthonyDesignCode
3 min readSep 11, 2022

--

Photo by Daniel Mingook Kim on Unsplash

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. publisherThe publisher to subscribe to.actionThe 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.

--

--

AnthonyDesignCode
AnthonyDesignCode

Written by AnthonyDesignCode

Hey 👋 I’m Anthony! I hope you are well?! Thank you for looking at my blog. Here you will learn how to make apps in SwiftUI for iOS 16! Thanks for reading.

No responses yet