所有UIViewControllers

所有UIViewControllers

我目前在情节提要中有3个UIViewController,并将它们连接到带有scrollView的一个UIPageViewController中。但是,由于没有在PageViewController中预加载所有UIViewControllers,因此在中心UIViewController上初始化应用程序时遇到了一些问题。当用户首次打开应用程序contentOffset时,我正在使用scrollview viewDidLoad来显示中心ViewController。

我已经在这里附上了我正在使用的代码:

    var currentIndex = 0
    var mainScrollView = UIScrollView()
    lazy var ViewControllerArray: [UIViewController] = {
        return [self.ViewControllerInstance(name: "RightVC"),
                self.ViewControllerInstance(name: "CenterVC"),
                self.ViewControllerInstance(name: "LeftVC")]

    }()

    private func ViewControllerInstance(name: String) -> UIViewController{

        return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)

    }


    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataSource = self
        self.delegate = self
        if let CenterViewController = ViewControllerArray.first {
            setViewControllers([CenterViewController] , direction: .reverse, animated: false, completion: nil)
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        mainScrollView = view.subviews.filter { $0 is UIScrollView }.first as! UIScrollView
        mainScrollView.delegate = self
        mainScrollView.contentOffset.x = self.view.bounds.size.width * 2



    }




    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let ViewControllerIndex = ViewControllerArray.index(of: viewController) else {
            return nil
        }

        let PreviousIndex = ViewControllerIndex - 1
        currentIndex = ViewControllerIndex
        guard PreviousIndex >= 0 else {
            return nil
        }
        guard ViewControllerArray.count > PreviousIndex else {
            return nil

        }

        return ViewControllerArray[PreviousIndex]
    }


    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let ViewControllerIndex = ViewControllerArray.index(of: viewController) else {
            return nil
        }

        let NextIndex = ViewControllerIndex + 1
        currentIndex = ViewControllerIndex
        guard NextIndex < ViewControllerArray.count else {
            return nil
        }
        guard ViewControllerArray.count > NextIndex else {
            return nil

        }

        return ViewControllerArray[NextIndex]

    }
    // Control bounce @ DidScroll & EndDragging
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let lastPosition = scrollView.contentOffset.x
        if (currentIndex == ViewControllerArray.count - 1) && (lastPosition > scrollView.frame.width) {
            scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)

        } else if currentIndex == 0 && lastPosition < scrollView.frame.width {
            scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
        }
    }
    // ^^
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        let lastPosition = scrollView.contentOffset.x
        if (currentIndex == ViewControllerArray.count - 1) && (lastPosition > scrollView.frame.width) {
            scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
        } else if currentIndex == 0 && lastPosition < scrollView.frame.width {
            scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
        }

    }

最佳答案

更新:
我使用变量IntialOpen作为系统的布尔值,以了解该应用程序之前是否已打开,并且还在公共函数中添加了If / else语句。

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let ViewControllerIndex = ViewControllerArray.index(of: viewController) else {
            return nil
        }
        let PreviousIndex = ViewControllerIndex - 1

        if !initialOpen {
            currentIndex = ViewControllerIndex
        }else{
            currentIndex = ViewControllerIndex + 1
            initialOpen = false
        }
        guard PreviousIndex >= 0 else {
            return nil
        }
        guard ViewControllerArray.count > PreviousIndex else {
            return nil
        }
        return ViewControllerArray[PreviousIndex]
    }


    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let ViewControllerIndex = ViewControllerArray.index(of: viewController) else {
            return nil
        }

        let NextIndex = ViewControllerIndex + 1


        if !initialOpen {
            currentIndex = ViewControllerIndex
        }else{
            currentIndex = ViewControllerIndex + 1
            initialOpen = false
        }
        guard NextIndex < ViewControllerArray.count else {
            return nil
        }
        guard ViewControllerArray.count > NextIndex else {
            return nil

        }

        return ViewControllerArray[NextIndex]

    }


如果它是第一次启动应用程序,则几乎迫使它在第二页上打开。

关于ios - 将所有UIViewControllers预加载到UIPageViewControllers中(快速),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44171230/

10-13 02:55