簡體   English   中英

將所有UIViewControllers預加載到UIPageViewControllers中(快速)

[英]Pre-load all UIViewControllers in UIPageViewControllers (Swift)

我目前在情節提要中有3個UIViewControllers,並將它們連接到帶有scrollView的一個UIPageViewController中。 但是,由於沒有在PageViewController中預加載所有UIViewController,我在中心UIViewController上初始化應用程序時遇到了一些問題。 當用戶第一次打開應用程序viewDidLoad時,我正在使用scrollview contentOffset來顯示中心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]

    }

如果它是第一次啟動應用程序,則幾乎可以迫使它在第二頁上打開。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM