I am trying to create an infinite scrolling paging UIScrollView
I have been following the Advanced UIScrollView Techniques video from WWDC 2010
however I am unsure as to how to create tiling for a paging UIScrollView
. I have been using this tutorial for guidance Infinite Paging along with this Stack Exchange answer. Is it possible to create such an effect in a paging UIScrollView
or is tiling primarily used only in a continuous scrolling environment. Thank you for your help.
Asked
Active
Viewed 246 times
0

Bhavesh Nayi
- 705
- 4
- 15

TheRedCamaro3.0 3.0
- 781
- 6
- 26
-
By tiling if you mean you have a horizaontal list of item which you want 360 scroll, you can append two extra views with first at last and the last one at first and can check for the offset value if it goes last(the first one) you change offset to the second one(the real first one) without any animations and similar for the last one, it makes it appear that user is scrolling infinitely. Its easier to achieve in collection view. – Amber K Apr 17 '19 at 05:45
1 Answers
1
If you need infinite scrolling you can change element position after scroll. very simple example and dirty code but I home I helped you:
import UIKit
class MainVC: UIViewController, UIScrollViewDelegate {
@IBOutlet var scrollView: UIScrollView!
var currentView: UIView?
var nextView: UIView?
var previousView: UIView?
override func viewDidLoad() {
super.viewDidLoad()
scrollView.isPagingEnabled = true
scrollView.delegate = self
generateViews()
}
func generateViews() {
let screenSize = UIScreen.main.bounds
currentView = UIView()
nextView = UIView()
previousView = UIView()
addPosition(cView: currentView!, nView: nextView!, pView: previousView!)
currentView?.backgroundColor = .red
nextView?.backgroundColor = .green
previousView?.backgroundColor = .blue
scrollView.addSubview(currentView!)
scrollView.addSubview(previousView!)
scrollView.addSubview(nextView!)
scrollView.contentSize = CGSize(width: screenSize.width * 3, height: screenSize.height)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x >= (UIScreen.main.bounds.width * 2) {
addPosition(cView: nextView!, nView: previousView!, pView: currentView!)
} else if scrollView.contentOffset.x == 0 {
addPosition(cView: previousView!, nView: currentView!, pView: nextView!)
}
scrollView.contentOffset = CGPoint(x: UIScreen.main.bounds.width, y: 0)
}
func addPosition(cView: UIView, nView: UIView, pView: UIView) {
let screenSize = UIScreen.main.bounds
cView.frame = CGRect(origin: CGPoint(x: screenSize.width, y: 0), size: screenSize.size)
nView.frame = CGRect(origin: CGPoint(x: screenSize.width * 2, y: 0), size: screenSize.size)
pView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: screenSize.size)
currentView = cView
nextView = nView
previousView = pView
}
}

felarmir
- 11
- 1
-
Thank you for taking the time to write such a detailed answer. I would like to swap in and out multiple view controllers on each view. How would you recommend adding and removing the view controllers so as not to have them all simultaneously initiated. – TheRedCamaro3.0 3.0 Apr 18 '19 at 01:36