How to create a UIScrollView of infinite scrolling

2019-01-20 14:05发布

问题:

  1. I have a UIScrollView of size 320*460 and with content size 1024*1024.
  2. I can place 25 images of 256*256 in it with the 13th picture shown at the centre of the screen when it loades with bits of surrounding pictures around it.
  3. When i swipe to any side I want it to appear just like the mapView. With new images appearing and showing.

How can I do it?

回答1:

It's very easy & somewhat tricky...

You don't need to define the specific size for the ScrollView.....

Generally we use to define ....as per the Examples of Apple

//#define SCROLLVIEW_CONTENT_HEIGHT 460

//#define SCROLLVIEW_CONTENT_WIDTH  320

which is of no use...if you need the infinite height..

just set the height of the ScrollView dynamically as per the Objects added to the ScrollView....

No need to set the predefined height for this...take the dynamic height....

scrollview.contentSize = CGSizeMake(320, txtView.contentSize.height+450);
 CGPoint topOffset = CGPointMake(0,0);
 [scrollview setContentOffset:topOffset animated:YES];

Hope this will surely work for you..

Good Luck :)



回答2:

Checkout the sample code called 'Tiling'. It might be what you're looking for.



回答3:

When scrolling stops, couldn't you just adjust the bounds so that you are now "re-centered" with your new offset?

Presumably you can't scroll more than so many pixels before your finger hits the edge of the screen, so you only need bounds that a few hundred pixels outside the original center.

Once you have a new center, adjust your bounds accordingly, retiling as necessary.