我已经设置了带有2个视图的PageViewControll。我可以在视图之间移动,pageControl(Dots)对应于正确的页面 - 但是点击点还不会滚动到正确的视图。
我在这里找到了关于如何创建函数但是无法实现成功使其工作的答案。页面控制器的代码如下(没有tap功能)完整的代码是:
func configurePageControl() {
pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 50,width: UIScreen.main.bounds.width,height: 50))
self.pageControl.numberOfPages = viewControllerList.count
self.pageControl.currentPage = 0
self.pageControl.alpha = 0.5
self.pageControl.tintColor = UIColor.white
self.pageControl.pageIndicatorTintColor = UIColor.black
self.pageControl.currentPageIndicatorTintColor = UIColor.white
self.view.addSubview(pageControl)
}
@IBAction func pageControltapped(_ sender: Any) {
guard let pageControl = sender as? UIPageControl else { return }
let selectedPage = pageControl.currentPage
self.setViewControllers([viewControllerList[selectedPage]], direction: .forward, animated: true, completion: nil)
}
}
谢谢你的帮助!!
pageControl
创建IBAction,您可以从中检测哪个点被点击。setViewControllers(_:direction:animated:completion:)
方法以编程方式滚动页面。
func setViewControllers(_ viewControllers: [UIViewController]?,
direction: UIPageViewController.NavigationDirection,
animated: Bool,
completion: ((Bool) -> Void)? = nil)
Here's the link to documentation使用以下代码:
@IBAction func pageControltapped(_ sender: Any) {
guard let pageControl = sender as? UIPageControl else { return }
let selectedPage = pageControl.currentPage
self.setViewControllers([viewControllerList[selectedPage]], direction: .forward, animated: true, completion: nil)
}
为pageControl添加目标以将其与IBAction链接:
func configurePageControl() {
//...your code as it is....
//add following line
self.pageControl.addTarget(self, action: #selector(pageControltapped(_:)), for: .touchUpInside)
}