菜鸟教程小白 发表于 2022-12-11 22:39:37

ios - 点击 pageControl 滚动到另一个 View (点击圆点)


                                            <p><p>我已经为 PageViewControll 设置了 2 个 View 。
我能够在 View 之间移动,并且 pageControl(点)对应于正确的页面 - 但是点击这些点还不能滚动到正确的 View 。 </p>

<p>我在这里找到了一些关于如何创建函数的答案,但未能成功实现使其工作。
页面 Controller 的代码如下(没有点击功能)
完整代码是:</p>

<pre><code>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(], direction: .forward, animated: true, completion: nil)
    }


    }
</code></pre>

<p>感谢您的帮助!!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><ol>
<li><p>您需要为 <code>pageControl</code> 创建 IBAction,您可以从中检测出哪个点被点击。 </p></li>
<li><p>然后,您需要使用 <code>setViewControllers(_:direction:animated:completion:)</code> 方法以编程方式滚动页面。</p>

<pre><code>func setViewControllers(_ viewControllers: ?,
      direction: UIPageViewController.NavigationDirection,
       animated: Bool,
   completion: ((Bool) -&gt; Void)? = nil)
</code></pre>

<p> <a href="https://developer.apple.com/documentation/uikit/uipageviewcontroller/1614087-setviewcontrollers" rel="noreferrer noopener nofollow">Here&#39;s the link to documentation</a> </p></li>
</ol>

<p><strong>使用以下代码:</strong></p>

<pre><code>@IBAction func pageControltapped(_ sender: Any) {
    guard let pageControl = sender as? UIPageControl else { return }
    let selectedPage = pageControl.currentPage
    self.setViewControllers(], direction: .forward, animated: true, completion: nil)
}
</code></pre>

<p><strong>为 pageControl 添加目标以将其与 IBAction 链接:</strong></p>

<pre><code>func configurePageControl() {
    //...your code as it is....
    //add following line
    self.pageControl.addTarget(self, action: #selector(pageControltapped(_:)), for: .touchUpInside)
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 点击 pageControl 滚动到另一个 View (点击圆点),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/55405731/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/55405731/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 点击 pageControl 滚动到另一个 View (点击圆点)