滑动方向 PageViewController

Swipe direction PageViewController

我的问题是使用 pageViewController 实现时的滑动动作。我希望它从右到左,但它是从左到右,谁不适合 UI 体验!

当我使用 Xcode 7 和 Swift 2.2 时,它工作得很好...... 因此,由于我 copied/pasted 我的代码并使用 Xcode 8 建议进行了修改,所以它有这种奇怪的行为。

import UIKit

class FirstEventViewController: UIViewController {

    // FIXME: - The swipe gesture has to be from right to left when the view appears (PageControl not showing)
    var pageViewController: UIPageViewController!
    var titleEvents = [String]()
    var pageImages: NSArray!

    override func viewDidLoad() {
        super.viewDidLoad()

        pageImages = NSArray(objects: "supersmashbros","spiritodjsession","thomasdelortrio")
        titleEvents = ["supersmashbros","spiritodjsession","thomasdelortrio"]

        initiatePgVC()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    // MARK: - Navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        //TODO: CODE HERE
    }

    // MARK: - Private functions
    /**
     Function initializing the logic of the pageviewController
    */

    func initiatePgVC() {
        pageViewController = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "PageViewController") as! UIPageViewController
        pageViewController.dataSource = self

        let startVC = viewControllerAtIndex(index: 0)
        let viewControllers = NSArray(object: startVC) as! [UIViewController]

        pageViewController.setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)

        //pageViewController.view.frame = CGRect(x:0, y:65, width:self.view.frame.width, height:self.view.frame.size.height - 140)
        pageViewController.view.frame = self.view.bounds

        addChildViewController(pageViewController)
        view.addSubview(pageViewController.view)
        pageViewController.didMove(toParentViewController: self)
    }

    /**
     Function associating the viewControllers
    */
    func viewControllerAtIndex(index: Int)->SecondEventViewController{
        if ((titleEvents.count == 0) || (index >= titleEvents.count)){
            return SecondEventViewController()
        }

        let vc:SecondEventViewController = storyboard?.instantiateViewController(withIdentifier: "SecondEventViewController") as! SecondEventViewController

        vc.imageFile = pageImages[index] as! String
        vc.labelTitle = titleEvents[index]
        vc.pageIndex = index

        return vc
    }
    }

    extension FirstEventViewController:UINavigationBarDelegate{
        func position(for bar: UIBarPositioning) -> UIBarPosition {
            return .topAttached
        }
        }

    extension FirstEventViewController:UIPageViewControllerDataSource{
        func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
            let vc = viewController as! SecondEventViewController
            var index = vc.pageIndex as Int
            if (index == 0 || index == NSNotFound){
                return nil
            }

        index = index-1
        return viewControllerAtIndex(index: index)
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        let vc = viewController as! SecondEventViewController
        var index = vc.pageIndex as Int
        if (index == NSNotFound){
            return nil
        }

        index = index+1
        if (index == titleEvents.count){
            return nil
        }
        return viewControllerAtIndex(index: index)

    }

    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
        return titleEvents.count
    }

    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
        return 0
    }

}

我曾经怀疑pageViewController.setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)因为它实现了方向但是即使我用reverse改变也没有任何反应。

你是不是搞错了索引的变化?

对于控制器之后,我希望索引会增加,但您正在减少它,反之亦然。

尝试交换它们,所以之后应该是 + 1,之前应该是 -1

index = index+1