Lazy loading pages in UIScrollView

I have a UIScrollView that contains large images, and I use paging to scroll between images. To save memory, I load only one image before and after the current visible one and loading / releasing new images after the scroll is completed.

The problem occurs when fast scrolling and scrollViewDidEndDecelerating not called.

How can I detect continuous scrolling?

I can check the location of an element on each scrollViewDidScroll , but this seems a bit heavy ...

+7
iphone cocoa-touch
source share
7 answers

Perhaps a table view with custom cell contents will work better for you, since it has a lot of logic built-in only to load cells, which are visible in contrast to everything at once. There are many guides to managing table views in various ways.

+2
source share

My temporary solution is to cancel continuous scrolling by disabling scrolling when the user lifts his finger until the scrolling is completed.

 -(void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView { [scrollView setScrollEnabled:NO]; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { [scrollView setScrollEnabled:YES]; } 
0
source share

You can do something like this

 -(void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat pageWidth = scrollView.frame.size.width; int page = floor((scrollView.contentOffset.x - pageWidth / 1.5) / pageWidth) + 1; } 

this method is called at any time when the contentoffset has changed programmatically or not.

From this method you can check if the "page" matches your current page, if not, you know that you need to load something. The trick here is that loading images does not hold scroll scrolling. This is where I am stuck.

page calculation will change to next / previous when you are halfway to the next page

0
source share

if you are still looking for a solution ... this is what I am doing, it works great with good performance.

 - (void)scrollViewDidEndDragging:(UIScrollView *)sv willDecelerate:(BOOL)decelerate { if (!decelerate) { // load images and unload the ones that are not needed anymore [self someMethod] } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)sv { // load images and unload the ones that are not needed anymore [self someMethod] } 
0
source share

You need to check both scrollViewDidEndDecelerating and scrollViewDidScroll. When the user clicks and releases to let the β€œmomentum” scroll, the first will be called in and. If the user decides to stop the scroll with his finger by clicking on the table (or scrolling approaches the very bottom, but I'm not sure about it) the second event is dismissed.

0
source share

According to my experience, @ jd291 is correct; I use the following callbacks for most places

 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView -(void)scrollViewDidScroll:(UIScrollView *)scrollView -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 

but the only case where a callback not called is that you might have incorrectly configured delegate .

0
source share

WWDC 2010 spoke about this. They reuse sub-scrolls, highlight the missing image in scrollViewDidScroll:. Video can help:

Session 104 - Scrolling List Application Development

0
source share

All Articles