Reputation: 598
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
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
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
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
Reputation: 1
For vertical scrolling i use this . Worked perfectly
Upvotes: 0
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
Reputation: 4546
You've got 3 options:
pagingEnabled
.setContentOffset: animated:
in the UIScrollViewDelegate
scrollViewDidEndDecelerating:
methodtouchesBegan
, touchesMoved
and touchesEnded
and extrapolate to create momentum with a appropriate endpointUpvotes: 11
Reputation: 2377
i think this may help you check this API provided by apple
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
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