UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Scrollview - snap in place/stop drifting...

Forums > SwiftUI

Hi, So Im trying to get something working, I think ive actually tried it a few times before on other projects and always gave up.

Would like to get a horizontal scrollview cell / card to sort of snap to place when it has moved onto the screen to stop the scrollview drifting. To give it a bit more of a pagestyle tabview feeling. (If that makes sense!)

The card is big enough that only one fits on the screen, so in my head it would make sense to call scrollTo to the cells id - with onAppear of the cell, but that doesn't seem to work, and does some odd things.

Maybe Im thinking about it all wrong and wondering if anyone has a solution?

edit: So I discovered why my onAppear wasn't working correctly - as I was using an HStack instead of a lazyHStack, and I didn't know that unlike List, scrollViews call onAppear for all elements at once. So anyway the scrollTo works as it should - but far too sensitive - jumps to the next cell, in a very jerky manner on the first hint of scrolling. I might just knock this on the head and give up, I wanted to have it to include a nice rotation3DEffect I had, but just seems not worth the hassle. Have switched over to page tabview.

3      

In case it matters, I'm facing the same problem. I have a set of pages that I scroll horizontally using a plain ScrollView because the user would like to support multi-page scrolling (swipe fast and scroll over many pages). What I have now is the plain "drifting" scroll and only after a full stop the view will calculate the nearest page and run scrollViewReader.scrollTo(nearestPage).

The problem is the time it takes to make a full stop. It's like watching curling. By the time the animation is done the user thinks it will not snap at all.

I am currently trying to make use of the drag.predictedEndLocation.x of a DragGesture to run scrollTo to the right page faster, but I don't know when to run this. Ideally, this should be when the drag is no longer active, but for other reasons the DragGesture's .onEnded is never called and I can't know for sure if the saved predictedEndLocation is going to be correct. it will also likely not be a "progressive swipe", as the speed at which the user moves his finger will likely not be the same as the animation caused by scrollTo.

Finally, TabView doesn't allow multi-page jumps.

Sigh.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.