How to stop a UIScrollView at a specific point?
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:
- You can do so using
pagingEnabled
.
- Use
setContentOffset: animated:
in theUIScrollViewDelegate
scrollViewDidEndDecelerating:
method
- Create your own scroll view using
touchesBegan
,touchesMoved
andtouchesEnded
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
}
Jesse Head
Updated on June 17, 2022Comments
-
Jesse Head almost 2 years
How do you stop a
UIScrollView
at specific point? That is, set the final position of theUIScrollView
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 over 12 yearsyeah... 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 over 12 yearsactually 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 over 12 yearsu can do by implementing its delegate methods, & using its offset value u can have either scroll more or stop scrolling...
-
vishy over 12 yearsuse - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated to scroll by code to a fixed position (offset), calculate the exact offset required..
-
Jesse Head over 12 yearsahh 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 over 12 yearsFinally someone is starting to understand what I'm looking for lol. number 2 is looking pretty appealing. will see how it works.
-
Jesse Head over 12 yearsWould you know how to detect the current position of the scrollview as the scrollViewDidEndDecelerating: method is triggered?
-
vishy over 12 yearsin didscrollend delegate u can do this... go through the delegate methods list once...
-
basvk over 12 yearsThe current position is stored in the
contentOffset
(CGPoint) property -
SleepNot about 10 years@JesseHead what did you end up using?
-
G.Abhisek about 8 years@JesseHead So what was your final answer?
-
Fattie almost 8 yearsnote that in many cases, you will also use
scrollViewDidScroll
- so that, the value will change as the user holds-and-moves the slider. -
Admin over 7 yearsHelped me out a ton!
-
budiDino over 5 yearsfor other n00bs like me, you can change
targetContentOffset
by updatingtargetContentOffset.pointee
to a newCGPoint
value ;)