gpt4 book ai didi

ios - 如何在启用分页的情况下实现 UIScrollView 平铺?

转载 作者:行者123 更新时间:2023-11-28 13:43:39 26 4
gpt4 key购买 nike

我正在尝试创建一个无限滚动的分页 UIScrollView 我一直在关注 Advanced UIScrollView Techniques来自 WWDC 2010 的视频,但是我不确定如何为分页 UIScrollView 创建平铺。我一直在使用这个教程作为指导Infinite Paging连同 this Stack Exchange answer .是否可以在分页 UIScrollView 中创建这样的效果,或者平铺主要仅用于连续滚动环境。感谢您的帮助。

最佳答案

如果您需要无限滚动,您可以在滚动后更改元素位置。非常简单的示例和肮脏的代码,但我回家了,我帮助了你:

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
}

}

关于ios - 如何在启用分页的情况下实现 UIScrollView 平铺?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55719034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com