Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Keep Scrolling Position on Resizing #212

Open
avenius opened this issue Feb 13, 2018 · 8 comments
Open

Suggestion: Keep Scrolling Position on Resizing #212

avenius opened this issue Feb 13, 2018 · 8 comments

Comments

@avenius
Copy link

avenius commented Feb 13, 2018

Is it possible to keep the current scrolling position or state after layout changes?

For example: scrolling is about 50%, the frame changes from (100pt x 20pt) to (101pt x 20pt). At the moment the animation restarts from the beginning. Maybe a property "keepScrollingPossition: Bool"

@cbpowell
Copy link
Owner

It's not possible at the moment, but that would definitely be a good feature! I've thought about it in the past but never tried implementing. I'll have to think about it again, and look up how things like scroll views handle it - that would probably be the best model to mimic.

@isaranjha
Copy link

isaranjha commented Jul 18, 2021

Is there any update on this? In my case it isn't resizing but the label seems to restart scrolling whenever a popover or modal view controller is presented. I don't know if it is related (would be helpful to know if so) but I've run into issues where the label doesn't show up (but is definitely added as a subview) until some sort of resizing/modal presentation occurs and then it pops up as visible. Not sure what's going on there... any ideas, @cbpowell?

I have found a sorta kinda hacky way to get around the labels constantly restarting:

override open func layoutSubviews() { super.layoutSubviews() if !awayFromHome { updateAndScroll() } }

Unfortunately does not address the bit about the labels not being visible onscreen (but added to the hierarchy), until I guess layoutSubviews is called again.

@cbpowell
Copy link
Owner

cbpowell commented Jul 18, 2021

I haven't put any time into trying to maintain scrolling position, but I do still think it would be a good feature! The problem is all the scroll animations are based on the home and away positions, which are in turn functions of the label's frame size. So if the frame size changes due to a rotation (or for any other reason) the animation would need to be adjusted in-flight so that the label goes to the correct position on that animation loop. Certainly not impossible, but requires more work than just commenting out a few lines!

@isaranjha your problems sound like possibly two separate issues, but at a high level MarqueeLabel tries to automatically catch when to perform its view layout and updates all of the require information at that point, but I guess it's missing however you're doing it? I won't claim to be totally up to date on all the latest iOS view layout mechanics, but I thought generally layoutSubviews() would be called on subviews. Also if your modals/popovers are causing a different view (like the underlying view) to trigger layout then yes that could potentially cause MarqueeLabel to reset.

Instead of that hack, perhaps use setNeedsLayout() or layoutIfNeeded() on your label instances to trigger a layout when you need it, and it's not automatically happening?

@isaranjha
Copy link

isaranjha commented Jul 18, 2021

I see. So I added the check for awayFromHome in order to solve issue #1 of the label restarting whenever there's a resize/presentation. It solved it well! Fingers crossed. Not ideal, but seems to work well in my use case even with rotations/etc. The most odd behavior I could see is it stopping for a quick second when it's actually at home, but that's not a huge deal in comparison it to it restarting and looking jerky. If something breaks, I'll be sure to let you know!

As far as issue #2, being that the label doesn't automatically show up until there's a resize/presentation, I tried setting setNeedsLayout() but it didn't budge, unfortunately. I do have the MarqueeLabel as a subview to another UIView but I agree that it should catch the layout and show up properly. I'll keep playing with it.

@isaranjha
Copy link

Welp! I solved it. Turns out I needed to call setNeedsLayout() on self.view. Strange but again, it works. 🤷🏻‍♂️

@cbpowell
Copy link
Owner

Weird, but nice! Glad you solved it.

@integral0909
Copy link

@cbpowell , @isaranjha
when my tabs are changed, labels restarts animation instead keeping scrolling position.
How can I keep scrolling positions when my tabs are changed?

@sxxjxxhxxn
Copy link

@integral0909
In my case, I use pauseLabel() at point starting any changes (state, layout ..)
And do unpauseLabel() when changes completed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants