OStack程序员社区-中国程序员成长平台

标题: ios - 滑动方向 PageViewController [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:16
标题: ios - 滑动方向 PageViewController

我的问题是使用 pageViewController 实现时的滑动 Action 。我希望它从右向左移动,但它是从左向右移动的,这对 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: "ageViewController") 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 改变,也没有任何反应。



Best Answer-推荐答案


您是否以错误的方式改变了索引?

对于 after controller,我希望索引会增加,但你正在减少它,反之亦然。

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

index = index+1

关于ios - 滑动方向 PageViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40420727/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4