Swift: ScrollView上拉滑动到下一页
2019-12-06 本文已影响0人
琪一可
前言:看看上次发布文章已经是三年之前了. 这三年生活和工作经历都是起起伏伏.幸运的是,对学会未知的东西,还是觉得很快乐.iOS这条路能走多久不知道.只是做好当下,把每天遇到的新tips整理一下吧.
需求: 滑动上一页,到底部时候,上拉滑动到下一页.
实现思路: 底部ScrollView,上面的内容也用ScrollView
要点1. 底部ScrollView.isPagingEnabled = true
要点2. scrollview准确设置contentSize
class ViewController: UIViewController {
var mainScrollView = UIScrollView()
var topScrollView = UIScrollView()
var bottomScrollView = UIScrollView()
override func viewDidLoad() {
super.viewDidLoad()
mainScrollView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
mainScrollView.contentSize = CGSize(width: 0, height: mainScrollView.frame.size.height * 2)
mainScrollView.isPagingEnabled = true
mainScrollView.delegate = self
view.addSubview(mainScrollView)
let scrollViewHeight = mainScrollView.frame.size.height
topScrollView.frame = CGRect(x: 0, y: 0, width: mainScrollView.frame.size.width, height: scrollViewHeight)
topScrollView.backgroundColor = .green
topScrollView.contentSize = CGSize(width: 0, height: scrollViewHeight * 1.5)
topScrollView.showsVerticalScrollIndicator = false
topScrollView.delegate = self
mainScrollView.addSubview(topScrollView)
bottomScrollView.frame = CGRect(x: 0, y: topScrollView.frame.size.height, width: mainScrollView.frame.size.width, height: mainScrollView.frame.size.height)
bottomScrollView.contentSize = CGSize(width: 0, height: scrollViewHeight)
bottomScrollView.backgroundColor = .orange
mainScrollView.addSubview(bottomScrollView)
}
}