6

I have a scrollview with 5 image views of width 88.

I want the scrollview to scroll to each image view (Not Paging)

and

I want to make an infinite scrollview in iPhone which means the when it scroll to last item it will display a first item next to it..

I have been trying by offset but it stops when move it by offset and also I have used apple street scroller which does not allow me to stop each element in center(just like Picker view)..

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
Asadullah Ali
  • 1,056
  • 14
  • 31

7 Answers7

9

First of all, I recommend to use a UITableView so you can maintain the memory usage low. An approach that I had used successfully in a project is the following:

    1. List item

Duplicate the content of your cells, I mean if you have 5 items in this way:

| 1 | 2 | 3 | 4 | 5 |

You should add the same (In a table view with reusable engine that's not a big deal) at the end of the table in order to look like this:

| 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 |

    2. modify the scrollViewDidScroll with the following:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _yourScrollView) {
        CGFloat currentOffsetX = scrollView.contentOffset.x;
        CGFloat currentOffSetY = scrollView.contentOffset.y;
        CGFloat contentHeight = scrollView.contentSize.height;

        if (currentOffSetY < (contentHeight / 6.0f)) {
            scrollView.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY + (contentHeight/2)));
        }
        if (currentOffSetY > ((contentHeight * 4)/ 6.0f)) {
            scrollView.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY - (contentHeight/2)));
        }
    }
}

The code above move the scroll position at top if you almost reach the final of the scrolling; Or if you are almost on the top, moves you to the bottom...

    3. That's it.
D33pN16h7
  • 2,030
  • 16
  • 20
  • 2
    Great solution. However is there any way to omit this 'blink' when offset is changed? – Nat Nov 21 '13 at 14:03
8

You can have a look at one of this two options:

Natan R.
  • 5,141
  • 1
  • 31
  • 48
4

This caused me serious problems for a long time so I really feel your pain. I found this control which should solve your problem: http://www.cocoacontrols.com/platforms/ios/controls/infinitescrollview

I also considered inserting transitions and disabled scroll so it becomes like a slideshow but either way should work.

Bradley
  • 617
  • 2
  • 11
  • 26
1

Here is a short explanation of solution I used to do it.

Lets say you have counter from 0 - 9, then, when you make transition 9 -> 0, you actually spoof another 0 (call it 0') after 9 (in scrollView), make animated transition to 0' and than make non-animated instant transition to 0 that stands on top of scrollView.

Ivor Prebeg
  • 988
  • 6
  • 11
0

You can use iCarousel library for this. https://github.com/nicklockwood/iCarousel

spaleja
  • 1,435
  • 15
  • 24
0

For anybody else looking for a solution, you can also try this library Infinite Scroll Picker at https://github.com/Seitk/InfiniteScrollPicker/ It's basically drag and drop. I'm using it on a project and it works great.

Storm Factory
  • 388
  • 2
  • 10
0

Implemented a simple infinity scroll in this mini project:

https://github.com/mcmatan/Infinity-tabBar-scroll-view/blob/master/README.md

MCMatan
  • 8,623
  • 6
  • 46
  • 85