如何在页面控件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上捕获点击事件的主要内容,如果未能解决你的问题,请参考以下文章

如何捕获android系统按键事件

Swift,如何在 AVPlayer 上播放视频期间获得事件点击完成按钮?

在android中怎样给整个页面设置监听事件

仅捕获不是由锚点点击引起的 hashchange 事件

在苹果 iphone 上,点击通知会捕获啥事件?

我可以在 Flex 中捕获键盘事件,而无需强制用户点击舞台吗?