2017-05-25 80 views
0

我目前在故事板中有3個UIViewControllers,並將它們連接到一個具有scrollView的UIPageViewController中。但是,由於未預先加載PageViewController中的所有UIViewController,因此我在初始化中心UIViewController上的應用程序時遇到了一些問題。當用戶第一次打開應用程序viewDidLoad時,我使用滾動視圖contentOffset來顯示中心ViewController。預加載UIPageViewControllers中的所有UIViewControllers(Swift)

我重視我使用此代碼:

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) 
     } 

    } 

回答

0

更新: 我用一個變量IntialOpen作爲系統中的布爾瞭解應用程序是否被打開之前,我還添加了如/ 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] 

    } 

如果第一次啓動應用程序,它的工作原理就是強迫它在第二頁上打開。

相關問題