滑动方向PageViewController

时间:2016-11-04 10:47:04

标签: ios xcode swift3 ios10 uipageviewcontroller

我的问题是我使用pageViewController实现时的滑动动作。我希望它从右到左,但是它从左到右,对于UI体验来说并不自然!

当我使用Xcode 7和Swift 2.2时,它工作得很好...... 因此,由于我使用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),因为它实现了方向,但即使我改变了反向,也没有任何反应。

1 个答案:

答案 0 :(得分:3)

你是否错误地改变了索引?

对于后控制器我希望指数增加但你正在减少它,反之亦然。

尝试交换这些,所以之后应该是+ 1和-1之前

index = index+1