gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:59:23 25 4
gpt4 key购买 nike

我已经设置了具有 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)
}


}

感谢您的帮助!!

最佳答案

  1. 您需要为 pageControl 创建 IBAction,您可以从中检测哪个点被点击。

  2. 然后,您需要使用 setViewControllers(_:direction:animated:completion:) 方法以编程方式滚动页面。

    func setViewControllers(_ viewControllers: [UIViewController]?, 
    direction: UIPageViewController.NavigationDirection,
    animated: Bool,
    completion: ((Bool) -> Void)? = nil)

    Here's the link to documentation

使用以下代码:

@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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com