joogps / Glur

A library for progressive blurs in SwiftUI.
https://apps.apple.com/app/glur/id6478063257
MIT License
1.3k stars 21 forks source link

LayerEffect doesn't work when applied to a ScrollView #1

Open Christopher-A opened 7 months ago

Christopher-A commented 7 months ago

From one of your older tweets (https://x.com/joogps/status/1667240291869270032?s=20), it looked like you were able to achieve this effect on content contained in a ScrollView. Is this supported with Glur?

joogps commented 7 months ago

Hey there, Christopher! IIRC for that tweet I made a custom implementation of a scrolling effect because, yes, UIKit-backed views don't work with Apple's SwiftUI shaders API currently, and ScrollView is one of those.

I guess I could file a radar for this given that scrolling is one of the best instances where this effect can be truly harnessed.

Christopher-A commented 7 months ago

Ah, makes sense. Thanks for the reply!

Christopher-A commented 7 months ago

Actually, I wonder if this is possible for ScrollViews with just a bit of hackery:

This should essentially deliver the effect in your earlier demo with the content blurring out as it is scrolled off the screen.

Christopher-A commented 7 months ago

I got a proof of concept working by applying the contentOffset to the position in mapRadius()... but the performance is horrendous, even returning 0 for any position outside of the visible bounds.

joogps commented 7 months ago

Actually, I wonder if this is possible for ScrollViews with just a bit of hackery:

  • Apply a GeometryReader to a background of a the ScrollView's content.
  • Use a preference to map the ContentView's GeometryProxy.frame.origin (in the ScrollViews coordinate space) to a State variable, to essentially get the contentOffset.
  • Pass the contentOffset to the shader to offset the blur, so it appears fixed in the ScrollView as the content scrolls.

This should essentially deliver the effect in your earlier demo with the content blurring out as it is scrolled off the screen.

That's really smart, but as you said, it's not really performant. Unfortunately (and possibly due to the UIKit backing of it), state updates within ScrollViews aren't handled well.

joogps commented 7 months ago

Filed a radar for this (FB13604715). Let's see how things go.

ostwilkens commented 7 months ago

Aww, I was sad when my ScrollView turned black. I want to use gradial blur as a background for my TabView bar, which is on top of a ScrollView... let's hope this gets fixed!