How to stop a UIScrollView at a specific point?

16,658

Solution 1

Take a look at method scrollViewWillEndDragging:withVelocity:targetContentOffset: of UIScrollViewDelegate. It's intended to do exactly what you need.

The scroll view sends this message to its delegate when the user finishes dragging the scroll view. The third parameter (targetContentOffset) is passed as a pointer, and you can change its value to change where the scroll view will stop.

iOS 5.0 and later.

Solution 2

You've got 3 options:

  1. You can do so using pagingEnabled.

  2. Use setContentOffset: animated: in the UIScrollViewDelegate scrollViewDidEndDecelerating: method

  3. Create your own scroll view using touchesBegan, touchesMoved and touchesEnded and extrapolate to create momentum with a appropriate endpoint

Solution 3

I've found something that works for me. It let's you scroll to point 0,0 but no further:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x <= -1) {
        [scrollView setScrollEnabled:NO];
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        [scrollView setScrollEnabled:YES];
    }
}

You could do the same for top, bottom or right (x or y)

Solution 4

to achieve this u need to enable the paging of the scrollview

For example:

[scrollView setContentSize:CGSizeMake(640, 161)];
[scrollView setPagingEnabled:YES];

here, width of scrollview is 640 (twice of width of iphone screen), if paging is enabled it will divide it equally depending on the width. So as we scroll to this position it will stop at the particular offset.

Solution 5

For those we need to set the targetContentOffset in Swift like rob mayoff says in his excellent answer above , this is the way :

func scrollViewWillEndDragging(scrollView: UIScrollView!, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    targetContentOffset.memory.y = x 
}
Share:
16,658
Jesse Head
Author by

Jesse Head

Updated on June 17, 2022

Comments

  • Jesse Head
    Jesse Head almost 2 years

    How do you stop a UIScrollView at specific point? That is, set the final position of the UIScrollView after user interaction?

    Specifically, how can you set intervals along a horizontal UIScrollView, so that the it will only stop at these points?

    I hope what I have said is clear enough.

  • Jesse Head
    Jesse Head over 12 years
    yeah... i looked at that originally, but i want to allow my content to stop at any number of positions after a swipe. paging only allows one set distance to be moved per swipe (in this case 320 pixels i believe?). this is hard to explain... if you have a look at the free news app called 'pulse' it shows what i am after.
  • vishy
    vishy over 12 years
    actually if the content size of scrollview is more then screen size, user can set to the fixed position as scrolling horiz/vert... do u want to do this by code..?
  • vishy
    vishy over 12 years
    u can do by implementing its delegate methods, & using its offset value u can have either scroll more or stop scrolling...
  • vishy
    vishy over 12 years
    use - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated to scroll by code to a fixed position (offset), calculate the exact offset required..
  • Jesse Head
    Jesse Head over 12 years
    ahh yeah, but wouldn't a separate event have to occur for the above method to be triggered? i want it to occur automatically after the scrolling has ended.
  • Jesse Head
    Jesse Head over 12 years
    Finally someone is starting to understand what I'm looking for lol. number 2 is looking pretty appealing. will see how it works.
  • Jesse Head
    Jesse Head over 12 years
    Would you know how to detect the current position of the scrollview as the scrollViewDidEndDecelerating: method is triggered?
  • vishy
    vishy over 12 years
    in didscrollend delegate u can do this... go through the delegate methods list once...
  • basvk
    basvk over 12 years
    The current position is stored in the contentOffset (CGPoint) property
  • SleepNot
    SleepNot about 10 years
    @JesseHead what did you end up using?
  • G.Abhisek
    G.Abhisek about 8 years
    @JesseHead So what was your final answer?
  • Fattie
    Fattie almost 8 years
    note that in many cases, you will also use scrollViewDidScroll - so that, the value will change as the user holds-and-moves the slider.
  • Admin
    Admin over 7 years
    Helped me out a ton!
  • budiDino
    budiDino over 5 years
    for other n00bs like me, you can change targetContentOffset by updating targetContentOffset.pointee to a new CGPoint value ;)