作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已经设置了具有 2 个 View 的 PageViewControll。我能够在 View 之间移动,并且 pageControl(点)对应于正确的页面 - 但是点击点不会滚动到正确的 View 。
我在这里找到了关于如何创建函数的几个答案,但无法成功实现以使其工作。页面 Controller 的代码如下(没有点击功能)完整的代码是:
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)
使用以下代码:
@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)
}
关于ios - 点击 pageControl 滚动到另一个 View (点击圆点),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55405731/
我是一名优秀的程序员,十分优秀!