SimultaneouslyScrollView icon indicating copy to clipboard operation
SimultaneouslyScrollView copied to clipboard

Delegate is overridden on `register(scrollView:)`

Open stonko1994 opened this issue 3 years ago • 2 comments

Current behaviour

When passing a UIScrollView to the SimultaneouslyScrollViewHandler using the register(scrollView:) method, a potentially already set delegate will be overridden.

Expected behaviour

An already set delegate should not be overridden when registering a UIScrollView.

stonko1994 avatar Mar 04 '22 13:03 stonko1994

Hi, is any idea to fix this issue? Thanks!

amoshsueh avatar Jan 13 '24 15:01 amoshsueh

One approach could be to store the delegate (if any) inside the SimultaneouslyScrollViewHandler and delegate all delegate calls to it.

stonko1994 avatar Jan 17 '24 10:01 stonko1994

There's actually a secondary problem with that: If you call register(scrollView:) twice (with a different scroll view handler), the synchronisation done by the first handler will stop working. Obviously because the delegate is overwritten.

This can be dealt with, but someone else setting a delegate can always be a problem. I's possible to deal with a delegate that's already set, but there's not much* that can be done about someone else overwriting the delegate later and breaking the scroll view synchronisation.

* technically it's possible to swizzle the delegate setter on UIScrollView.

julasamer avatar Oct 09 '24 09:10 julasamer

I made another discovery: SwiftUI already sets a delegate on UIScrollView called SwiftUI.ScrollViewHelper. I didn't find any public documentation on what that does, but just overwriting it probably breaks something.

julasamer avatar Oct 09 '24 11:10 julasamer

SwiftUI already sets a delegate on UIScrollView called SwiftUI.ScrollViewHelper.

that's interesting 👀. wasn't aware of this 🤔 maybe it's time to keep the original delegate and proxy through to it.

stonko1994 avatar Oct 09 '24 15:10 stonko1994

I've implemented a solution; maybe I have time to create a pull request later in the evening. It doesn't help if someone else sets the delegate later on though, but in my brief testing that hasn't happened.

julasamer avatar Oct 09 '24 16:10 julasamer

that would be awesome 🙌

stonko1994 avatar Oct 09 '24 16:10 stonko1994

Took me a day longer, but I've submitted my pull request.

By the way, the reason why I started to investigate is because of my use case: I basically have a layout like in a spreadsheet application. A scrollview for the row headers that only scroll vertically, a scrollview for the column headers that only scroll horizontally, and a grid of cells that scroll both ways. The grid of cells needs to be synced to two other scroll views using two different handlers. That didn't work prior to adding the multicast delegate stuff.

julasamer avatar Oct 10 '24 20:10 julasamer