Scroll to page programmatically
Opened this issue · 3 comments
Hi Jesse,
we are using CubeView extensively as one of our main UI assets. However, when it comes to the details, I'm struggling to understand your implementation, mostly because I have only a very basic understanding of CoreAnimation.
Could you hint me in a direction of how to programmatically scroll the cube to the previous/next page?
I basically get it to work by exposing the scrollView-property and using scrollRectToVisible:Animated: to scroll to multiples of the bound-width. However, this fails when there are more than 3 pages.
I'm guessing it has a lot to do with:
if ([delegate numPagesForCubeView:self] > 2) {
self.scrollView.contentSize = CGSizeMake(self.bounds.size.width * 3, self.bounds.size.height);
I think I also have a basic misconception of how the cube animation and the scrollView are coupled.
So again my question:
Could you hint me in a direction of how to programmatically scroll the cube to the previous/next page?
Ideally, how could I implement this:
- (void)scrollCubeViewToPage:(int)page animationDuration:(NSTimeInterval)duration
Thanks a million times for your help!
Cheers
Chris
Hi Chris,
You're exactly right about why simply setting scrollRectToVisible: won't work. The CubeView simulates a very long scrolling list using a short scrollView, hopefully enabling lists that could potentially be thousands of pages long without memory concerns. Essentially: The view that you are observing is at the center of the three-page scrollView. When you flip to the view above or below, CubeView will rearrange the views once the new view is fully on-screen, so that the view you are looking at is again in the middle of the scrollView.
If you'd like to simply move forward of backward by one page, that should be fairly simple to do. Considering that with the exception of the edges of your list, you should be at the middle page in your scrollView, you can always scroll one page away from here and things should work if all of the appropriate scrolling UIScrollViewDelegate callbacks get called. Further scrolling gets a bit complicated, and what you're looking for should probably be considered from a user experience point of view as well -- endlessly spinning the cube might be a peculiar experience. The best option might be a simulation of the spinning to arrive at another page, then simply set the page number and reload the view.
Not sure if that's helpful. Please get back to me with more questions if needed!
Hi @jexe !
Have you managed to get it working?
I'm trying to achieve the same result, scroll to the right/left face of the cube programmatically; if you had some code to share it would be great!
Thank you,
DAN
please add code for programmatically scroll.