如何在页面控件swift上捕获点击事件
Posted
技术标签:
【中文标题】如何在页面控件swift上捕获点击事件【英文标题】:How to Capture click event on Page Control swift 【发布时间】:2019-02-06 11:04:27 【问题描述】:我遵循有关如何创建UIPageControl
的教程,当我单击页面指示器时,它不会更改页面
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else
return nil
let previousIndex = viewControllerIndex - 1
// User is on the first view controller and swiped left to loop to
// the last view controller.
guard previousIndex >= 0 else
return orderedViewControllers.last
// Uncommment the line below, remove the line above if you don't want the page control to loop.
// return nil
guard orderedViewControllers.count > previousIndex else
return nil
return orderedViewControllers[previousIndex]
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else
return nil
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
// User is on the last view controller and swiped right to loop to
// the first view controller.
guard orderedViewControllersCount != nextIndex else
return orderedViewControllers.first
// Uncommment the line below, remove the line above if you don't want the page control to loop.
// return nil
guard orderedViewControllersCount > nextIndex else
return nil
return orderedViewControllers[nextIndex]
func configurePageControl()
// The total number of pages that are available is based on how many available colors we have.
pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 40,width: UIScreen.main.bounds.width,height: 40))
self.pageControl.numberOfPages = orderedViewControllers.count
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.gray
self.pageControl.pageIndicatorTintColor = UIColor.gray
self.pageControl.currentPageIndicatorTintColor = UIColor.white
self.view.addSubview(pageControl)
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool)
let pageContentViewController = pageViewController.viewControllers![0]
self.pageControl.currentPage = orderedViewControllers.index(of: pageContentViewController)!
【问题讨论】:
检查这个:***.com/questions/13854222/… 【参考方案1】:在configurePageControl()
中添加pageControl.addTarget
func configurePageControl()
// The total number of pages that are available is based on how many available colors we have.
pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 40,width: UIScreen.main.bounds.width,height: 40))
//add target for dots
pageControl.addTarget(self, action: #selector(self.pageControlSelectionAction(_:)), for: .touchUpInside)
self.pageControl.numberOfPages = orderedViewControllers.count
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.gray
self.pageControl.pageIndicatorTintColor = UIColor.gray
self.pageControl.currentPageIndicatorTintColor = UIColor.white
self.view.addSubview(pageControl)
然后添加点被选中时换页的方法。
@objc func pageControlSelectionAction(_ sender: UIPageControl)
//move page to wanted page
let page: Int? = sender.currentPage
self.pageViewController?.setViewControllers([[orderedViewControllers[page!]]], direction: .forword, animated: true, completion: nil)
【讨论】:
您好,我尝试了此解决方案,但出现此错误“如果没有上下文类型,则无法解析对成员 'setViewControllers' 的引用”。【参考方案2】:在我的情况下(Xcode 13),我收到了以前的 currentPage。 所以使用 dispatchQueue asyncAfter
@objc func pageControlSelectionAction(_ sender: UIPageControl)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute:
let page: Int? = sender.currentPage
//scroll
另外,我刚刚测试了“DispatchQueue.main.async”也没问题。
【讨论】:
以上是关于如何在页面控件swift上捕获点击事件的主要内容,如果未能解决你的问题,请参考以下文章