最佳答案
首先在UIPageViewController中找到scrollview并添加UIScrollViewDelegate
for view in self.pageViewController!.view.subviews {
if let subView = view as? UIScrollView {
subView.delegate = self
subView.isScrollEnabled = true
subView.bouncesZoom = false
}
}
(在ViewController类中扩展UIScrollViewDelegate)
对于方法“scrollViewDidScroll”和“scrollViewWillEndDragging”上的UIScrollViewDelegate,可以添加类似的内容
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if(currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width){
scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
}else if(currentIndex == 2 && scrollView.contentOffset.x > scrollView.bounds.size.width) {
scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
}
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if(currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width){
scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
}else if(currentIndex == 2 && scrollView.contentOffset.x > scrollView.bounds.size.width) {
scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
}
}