BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News SwiftUI 5 Leaves Combine behind, Extends Animations, and More

SwiftUI 5 Leaves Combine behind, Extends Animations, and More

At WWDC 2023, Apple presented the latest iteration of its declarative framework for UI programming. SwiftUI 5 completely overhauls its reactive foundations by replacing Combine with the new Observation framework. Additionally, it adds support for phased animations, improves ScrollView, new gestures, and more.

Adopting the new Observation framework brings a number of benefits, including simplified syntax and streamlined semantics, the possibility of tracking optionals and collections, and extending the usage of State and Environment to replace StateObject and EnvironmentObject. The Observation framework also allows to do away with the requirement of annotating each observable property using the @Published property wrapper. Instead, you use the @Observable macro at the class-level to make all properties of that class observable, as shown in the following example:

@Observable class Library {
    var books: [Book] = [Book(), Book(), Book()]
}

For the special case where you want to avoid observing a given property, you can use the ObservationIgnored macro with that specific property. Interestingly, for all developers having apps that use SwiftUI and Combine, the adoption of the new framework can be carried through incrementally.

The Observation framework is undoubtedly a major innovation that will change the way developers structure their SwiftUI code. The latest release of SwiftUI also includes, though, incremental improvements to the framework aimed to extend its capabilities or catch up with features developers took for granted in UIKit.

A relatively minor but surely welcome new feature is the possibility of tracking the scrollPosition of a ScrollView. When a view is scrolled to a new position, the scrollPosition modifier will update a state variable identifying the first visible view. Conversely, you can assign a value to that state variable to jump to any given view. Additionally, you can now choose a scroll behavior using the new ScrollTargetBehaviour protocol.

SwiftUI animations have got a subtle but powerful extension, too, thanks to the introduction of completion handlers. While apparently minor, this new feature enables the definition of phased animations, i.e., animations that go through a sequence of states or phases, possibly in a loop.

Instead of building a phased animation by manually concatenating individual phases directly using their completion handlers, you can also use the new PhaseAnimator view to iterate over the sequence of phases and defining a specific animation for each phase.

SwiftUI 5 also introduces two new gestures, RotateGesture and MagnifyGesture, to track a view's rotation and magnification. RotateGesture replaces the old RotationGesture, while MagnifyGesture replaces the old MagnificationGesture. The new classes provide a streamlined syntax which makes it easier to apply rotation and magnification transformations to a view.

The new features will be available starting with iOS 17, iPadOS 17, macOS 17, and the rest of Apple OSes that will be release next Fall. For a complete overview of what's new in SwiftUI, do not miss Apple's presentation at WWDC 2023.

About the Author

Rate this Article

Adoption
Style

BT