[英]How do I implement UIScrollView tiling with paging enabled?
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
. 我试图创建一个无限滚动的页面
UIScrollView
我一直在关注WWDC 2010
的Advanced UIScrollView Techniques视频,但是我不确定如何为页面UIScrollView
创建切片。 I have been using this tutorial for guidance Infinite Paging along with this Stack Exchange answer . 我一直在使用本教程来指导Infinite Paging以及Stack Exchange的答案 。 Is it possible to create such an effect in a paging
UIScrollView
or is tiling primarily used only in a continuous scrolling environment. 是否可以在分页
UIScrollView
创建这样的效果,还是仅主要在连续滚动环境中使用的平铺。 Thank you for your help. 谢谢您的帮助。
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.