Pageview scrollview不是锻炼

时间:2018-02-17 09:38:35

标签: ios swift uiscrollview

这是我的pageView代码。它不会滚动。我使用的是swift2,因为我的设置中没有swift4。任何人都可以帮助我,我是新手!

import UIKit
class TutorialPageViewController:UIPageViewController,UIPageViewControllerDataSource,UIPageViewControllerDelegate
{

     //Storyboard class and id - 
     //ViewController1,ViewController2,ViewController3

    private(set) lazy var orderedViewControllers: [UIViewController] = {
         return [self.newColoredViewController("ViewController1"),
         self.newColoredViewController("ViewController2"),
         self.newColoredViewController("ViewController3")]
   }()    

 private func newColoredViewController(name: String) -> UIViewController {         
  return UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("name")
}

    //code of pageview

    override func viewDidLoad() {
   super.viewDidLoad()
   self.dataSource = self
   self.delegate = self

   if let ViewController1 = orderedViewControllers.first {
                    setViewControllers([ViewController1],
                        direction: .Forward,
                        animated: true,
                        completion: nil)

   }

 }

1 个答案:

答案 0 :(得分:0)

您还需要实现UIPageViewControllerDataSource:

extension Yourcontroller: UIPageViewControllerDataSource{


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

        let previousIndex = viewControllerIndex - 1

        // User is on the first view controller and swiped left to loop to
        // the last view controller.
        guard previousIndex >= 0 else {
            return orderedViewControllers.last
            // Uncommment the line below, remove the line above if you don't want the page control to loop.
            //return nil
        }

        guard orderedViewControllers.count > previousIndex else {
            return nil
        }

        return orderedViewControllers[previousIndex]

    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
            return nil
        }
        let nextIndex = viewControllerIndex + 1


        let orderedViewControllersCount = orderedViewControllers.count

        // User is on the last view controller and swiped right to loop to
        // the first view controller.
        guard orderedViewControllersCount != nextIndex else {
            return orderedViewControllers.first
            // Uncommment the line below, remove the line above if you don't want the page control to loop.
            //return nil
        }

        guard orderedViewControllersCount > nextIndex else {
            return nil
        }

        return orderedViewControllers[nextIndex]
    }
}