show me a good use case for onScrollGeometryChange. I want to use it to put the scrollOffset in the environment, but i think that might be too expensive
Generated on 8/2/2024
1 search
Using onScrollGeometryChange
to put the scrollOffset
in the environment might indeed be too expensive, especially if you are looking for a performant solution. Instead, you might want to consider using the scroll transition
and visual effect
modifiers in SwiftUI, which are designed to create custom scroll effects in a more efficient manner.
Use Cases for onScrollGeometryChange
-
Creating Parallax Effects:
- You can use scroll transitions to change the x offset of an image while keeping the shape that clips it unchanged. This can create a parallax effect, making the scroll view feel more dynamic.
- Example: Create custom visual effects with SwiftUI (00:03:10)
-
Customizing Visual Properties:
- You can change various visual properties like scale, fade, and blur based on the y position of a shape as it gets to the top of the scroll view.
- Example: Create custom visual effects with SwiftUI (00:05:08)
-
Gradient Effects:
- By using the location of the view from the proxy, you can change the hue of your view to create a gradient effect. The lower the view on the device, the stronger the hue rotation.
- Example: Create custom visual effects with SwiftUI (00:04:07)
Recommendations
- Scroll Transitions: These are great for creating interesting and unique scroll experiences without the overhead of constantly updating the environment.
- Visual Effect Modifiers: These provide access to a content placeholder and a proxy, allowing you to change visual properties based on the view's position and size in a performant way.
For more detailed examples and to see these techniques in action, you can refer to the session Create custom visual effects with SwiftUI.
Relevant Sessions
These sessions provide a comprehensive overview of how to use scroll transitions and visual effect modifiers effectively in SwiftUI.
What’s new in SwiftUI
Learn how you can use SwiftUI to build great apps for any Apple platform. Explore a fresh new look and feel for tabs and documents on iPadOS. Improve your window management with new windowing APIs, and gain more control over immersive spaces and volumes in your visionOS apps. We’ll also take you through other exciting refinements that help you make expressive charts, customize and layout text, and so much more.
Enhance your UI animations and transitions
Explore how to adopt the zoom transition in navigation and presentations to increase the sense of continuity in your app, and learn how to animate UIKit views with SwiftUI animations to make it easier to build animations that feel continuous.
Migrate your TVML app to SwiftUI
SwiftUI helps you build great apps on all Apple platforms and is the preferred toolkit for bringing your content into the living room with tvOS 18. Learn how to use SwiftUI to create familiar layouts and controls from TVMLKit, and get tips and best practices.
Create custom visual effects with SwiftUI
Discover how to create stunning visual effects in SwiftUI. Learn to build unique scroll effects, rich color treatments, and custom transitions. We’ll also explore advanced graphic effects using Metal shaders and custom text rendering.