Jesse Head
Jesse Head

Reputation: 598

How to stop a UIScrollView at a specific point?

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.

Upvotes: 10

Views: 17750

Answers (9)

samwize
samwize

Reputation: 27383

This will work just fine:

override func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView.contentOffset.y > CERTAIN_POINT {
        scrollView.scrollEnabled = false
        scrollView.scrollEnabled = true
    }
}

Upvotes: 2

Victor Sigler
Victor Sigler

Reputation: 23449

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 
}

Upvotes: 2

Yoko
Yoko

Reputation: 803

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)

Upvotes: 4

user3327570
user3327570

Reputation: 1

For vertical scrolling i use this . Worked perfectly

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView { self.scrollView.contentOffset = CGPointMake(0, desiredPoint.y); }

Upvotes: 0

rob mayoff
rob mayoff

Reputation: 386018

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.

Upvotes: 35

basvk
basvk

Reputation: 4546

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

Upvotes: 11

B25Dec
B25Dec

Reputation: 2377

i think this may help you check this API provided by apple

  • (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated

And apple clearly said about this This method scrolls the content view so that the area defined by rect is just visible inside the scroll view. If the area is already visible, the method does nothing.

Upvotes: 0

Tendulkar
Tendulkar

Reputation: 5540

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

Upvotes: 0

vishy
vishy

Reputation: 3231

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.

Upvotes: 2

Related Questions