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
?
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:
What it does is exactly what you need, and it's far better than hacky
contentOffset
From: https://developer.apple.com/documentation/uikit/uiscrollview/1619439-scrollrecttovisible
You can scroll to some point in a scroll view with one of the following statements in Objective-C
or Swift
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.
Another way is
If you want control over the duration and style of the animation, you can do:
Adjust the duration (
2.0f
) and options (UIViewAnimationOptionCurveLinear
) to taste!