roy*_*nyc 6 ios uipageviewcontroller swift swift4
我已经设置了具有 2 个视图的 PageViewControll。我可以在视图之间移动,并且 pageControl(点)对应于正确的页面 - 但是点击点还不能滚动到正确的视图。
我在这里找到了一些关于如何创建该函数的答案,但无法成功实现以使其工作。页面控制器的代码如下(没有点击功能)完整的代码是:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!!
您需要创建 IBAction,pageControl从中可以检测到哪个点被点击。
然后,您需要使用setViewControllers(_:direction:animated:completion:)方法以编程方式滚动页面。
func setViewControllers(_ viewControllers: [UIViewController]?,
direction: UIPageViewController.NavigationDirection,
animated: Bool,
completion: ((Bool) -> Void)? = nil)
Run Code Online (Sandbox Code Playgroud)
使用以下代码:
@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)
}
Run Code Online (Sandbox Code Playgroud)
添加 pageControl 的目标以将其与 IBAction 链接:
func configurePageControl() {
//...your code as it is....
//add following line
self.pageControl.addTarget(self, action: #selector(pageControltapped(_:)), for: .touchUpInside)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4717 次 |
| 最近记录: |