views:

348

answers:

3

Well, there is the delegate that will get called when:

  • the scroll view beginns scrolling
  • the finger liftet of from the scroll view after initializing a scroll-movement
  • the scroll view stopped decelerating

but now the problem is, if you want to recycle those View's upon scrolling, you must do that very quickly during scrolling. Something must trigger a method that will either remove one or three views from top to bottom, or from bottom to top, as the user scrolls.

Problems:

  • The user might scroll very slowly, or very fast. I don't know.

  • As soon as scrolling begins, I must check i.e. 50 times per second what the offset is, and determine if it's time to recycle views or not. Next problem is, how to invoke a method 50 times per second, and how to stop that as scrolling stopps.

Which patterns do you know to solve that problem? I am sure that there are some good ones out there.

+2  A: 

Table views are excellent for this kind of thing, and very flexible.

Kare Morstol
+1  A: 

As a matter of fact, scrollViewDidScroll method is called continuously during scrolling, not when the finger is lifted. (Actually it is called every time contentOffset is changed, be it because of user scrolling or a programmatic change.) So you can safely use it to recycle the views.

Be sure to preload the views within one screen size distance from the current view, because the user can potentially scroll one screen width or height distance in one gesture.

And of course, if you were going to arrange your views vertically, UITableView is your friend and does the recycling for you. (It also supports variable-height rows, if you are interested.)

Andrey Tarantsov
Thanks! It can also happen that the user snaps quickly, and then it would scroll several pages down or up in one turn, until it finishes decelerating. I didn't use UITableView because I need total control of the UIImageView's in the scroll view. That is, because I've implemented some special effects that happen during scrolling.
Thanks
You are right. What I meant that the user can probably scroll one screen's height worth of data with a single touch, so that your delegate won't be called during that process. I believe you are guaranteed to receive a call to scrollViewDidScroll at least once per each screen height of scrolling made by the user. (At least that's a reasonable assumption to make.) Of course, these are extreme cases we're talking about.
Andrey Tarantsov
BTW can you explain in more detail what exactly are you doing that cannot be done with a table view? Just wondering.
Andrey Tarantsov
+1  A: 

If paging is enabled then your delegate receives a

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

when scrolling is at a page boundary which is a good time to recycle views. Otherwise you will have to monitor content offset in the delegate's

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;

and work out when you can recycle a view.

Roger Nolan
Thanks! I used the - (void)scrollViewDidScroll:(UIScrollView *)scrollView; for this.
Thanks