Programmatically scroll a UIScrollView
Solution 1
You can scroll to some point in a scroll view with one of the following statements in Objective-C
[scrollView setContentOffset:CGPointMake(x, y) animated:YES];
or Swift
scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true)
See the guide "Scrolling the Scroll View Content" from Apple as well.
To do slideshows with UIScrollView
, you arrange all images in the scroll view, set up a repeated timer, then -setContentOffset:animated:
when the timer fires.
But a more efficient approach is to use 2 image views and swap them using transitions or simply switching places when the timer fires. See iPhone Image slideshow for details.
Solution 2
If you want control over the duration and style of the animation, you can do:
[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
scrollView.contentOffset = CGPointMake(x, y);
} completion:NULL];
Adjust the duration (2.0f
) and options (UIViewAnimationOptionCurveLinear
) to taste!
Solution 3
I'm amazed that this topic is 9 years old and the actual straightforward answer is not here!
What you're looking for is scrollRectToVisible(_:animated:)
.
Example:
extension SignUpView: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
scrollView.scrollRectToVisible(textField.frame, animated: true)
}
}
What it does is exactly what you need, and it's far better than hacky contentOffset
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.
From: https://developer.apple.com/documentation/uikit/uiscrollview/1619439-scrollrecttovisible
Solution 4
Another way is
scrollView.contentOffset = CGPointMake(x,y);
Solution 5
With Animation in Swift
scrollView.setContentOffset(CGPointMake(x, y), animated: true)
Related videos on Youtube
climbon
Updated on February 14, 2021Comments
-
climbon over 3 years
I have a
UIScrollView
which has several views. When a user flicks their finger, the view scrolls to the right or left depending on the direction of the finger flick. Basically my code works in a way similar to the iPhone photo app. Now, is there a way that I can programmatically do the same thing so that I end up with a slideshow that runs on its own with a click of a button and a configurable pause between each scroll?How do you really do slideshows with
UIScrollView
? -
climbon over 14 yearsCool. Yes, I did find that setContentOffset works but really wanted this to happen in animated way. 'animated:YES' did the trick.
-
Evan Mulawski about 12 yearsThis is exactly the same as the accepted answer. And
CGPoint
should beCGPointMake
. -
Giovanni almost 12 yearsJust complementing the answer, to move horizontally to the next "page" in UIScrollView (assuming you are coding for iPhone), for the x parameter use
(myScrollView.contentOffset.x +320)
. -
DD_ over 11 years@niraj thanks dude...in
(myScrollView.contentOffset.x +320)
lies the key! -
Chris over 10 yearsCorrect me if I'm wrong, but UIScrollView's
contentOffset:
will offset the contentSize as well, which may not be desirable. To only scroll, you may want to usescrollRectToVisible:
. -
user1781290 almost 10 yearsPlease consider adding some text to your answer, not only pure code
-
quemeful over 9 yearsI like simple answers like this one.
-
Arnold almost 9 yearsDo no use C helper functions like
CGPointMake
in Swift. Simply build a Swift struct directly:CGPoint(x: 0, y: 44)
-
Rickster over 8 yearsActually, this is not the same as the accepted answer. The accepted answer has an option of animated, which some apps might want to have set to YES.
-
Abhishek Thapliyal almost 8 years[scrollView scrollRectToVisible:CGRectMake(x, y, width, height), animated:YES]; when i'm adding this it is slightly scrolled upwards initially. Can you give me a fix @kennytm ?
-
Abdurakhmon over 2 yearsgood solution, but contentOffset gives more control over where exactly you want to scroll