禁用 UIPageViewController 弹跳 - Swift [重复]

Posted

技术标签:

【中文标题】禁用 UIPageViewController 弹跳 - Swift [重复]【英文标题】:Disable UIPageViewController bouncing - Swift [duplicate] 【发布时间】:2015-06-21 11:10:08 【问题描述】:

我一直在制作一个在UIViewControllers 之间滚动的示例应用程序,但关键是我想在滚动结束时以及返回第一个UIViewController 时禁用弹跳效果。

这是我的代码:

class PageViewController: UIPageViewController,UIPageViewControllerDataSource, UIPageViewControllerDelegate

    var index = 0
    var identifiers: NSArray = ["FirstNavigationController", "SecondNavigationController"]
    override func viewDidLoad() 

        self.dataSource = self
        self.delegate = self

        let startingViewController = self.viewControllerAtIndex(self.index)

        let viewControllers: NSArray = [startingViewController]
        self.setViewControllers(viewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
    

    func viewControllerAtIndex(index: Int) -> UINavigationController! 

        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        //first view controller = firstViewControllers navigation controller
        if index == 0 

            return storyBoard.instantiateViewControllerWithIdentifier("FirstNavigationController") as! UINavigationController

        

        //second view controller = secondViewController's navigation controller
        if index == 1 

            return storyBoard.instantiateViewControllerWithIdentifier("SecondNavigationController") as! UINavigationController
        

        return nil
    
    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? 

        let identifier = viewController.restorationIdentifier
        let index = self.identifiers.indexOfObject(identifier!)

        //if the index is the end of the array, return nil since we dont want a view controller after the last one
        if index == identifiers.count - 1 

            return nil
        

        //increment the index to get the viewController after the current index
        self.index = self.index + 1
        return self.viewControllerAtIndex(self.index)

    

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? 

        let identifier = viewController.restorationIdentifier
        let index = self.identifiers.indexOfObject(identifier!)

        //if the index is 0, return nil since we dont want a view controller before the first one
        if index == 0 

            return nil
        

        //decrement the index to get the viewController before the current one
        self.index = self.index - 1
        return self.viewControllerAtIndex(self.index)

    


看看这里:

【问题讨论】:

【参考方案1】:

我不知道你是否可以。 UIPageController 不是很可定制。

就个人而言,当我想在 UIViewController 之间滚动时,我更喜欢使用一个简单的 UIViewController,它是一个容器,里面有一个 UIScrollView。然后我以编程方式在 UIScrollView 的 contentSize 中添加所有控制器。您必须将所有控制器添加为容器的子级。

更新 iOS 9

func setupDetailViewControllers() 
    var previousController: UIViewController?
    for controller in self.controllers 
        addChildViewController(controller)
        addControllerInContentView(controller, previousController: previousController)
        controller.didMoveToParentViewController(self)
        previousController = controller
    


func addControllerInContentView(controller: UIViewController, previousController: UIViewController?) 
    contentView.addSubview(controller.view)
    controller.view.translatesAutoresizingMaskIntoConstraints = false

    // top
    controller.view.topAnchor.constraintEqualToAnchor(contentView.topAnchor).active = true

    // bottom
    controller.view.bottomAnchor.constraintEqualToAnchor(contentView.bottomAnchor).active = true

    // trailing
    trailingContentViewConstraint?.active = false
    trailingContentViewConstraint = controller.view.trailingAnchor.constraintEqualToAnchor(contentView.trailingAnchor)
    trailingContentViewConstraint?.active = true

    // leading
    let leadingAnchor = previousController?.view.trailingAnchor ?? contentView.leadingAnchor
    controller.view.leadingAnchor.constraintEqualToAnchor(leadingAnchor).active = true

    // width
    controller.view.widthAnchor.constraintEqualToAnchor(scrollView.widthAnchor).active = true

以前的答案

像这样:

scrollView 是一个 IBOutlet,对 ContainerViewController 的每个边缘都有约束

class ContainerViewController: UIViewController 

@IBOutlet var scrollView: UIScrollView!

override func viewDidLoad() 
    super.viewDidLoad()
    scrollView.bounces = false
    scrollView.pagingEnabled = true


override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()



override func viewDidLayoutSubviews() 
    initScrollView()


func initScrollView()
    let viewController1 = storyboard?.instantiateViewControllerWithIdentifier("ViewController1") as! ViewController1

    viewController1.willMoveToParentViewController(self)
    viewController1.view.frame = scrollView.bounds

    let viewController2 = storyboard?.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2

    viewController2.willMoveToParentViewController(self)
    viewController2.view.frame.size = scrollView.frame.size
    viewController2.view.frame.origin = CGPoint(x: view.frame.width, y: 0)

    scrollView.contentSize = CGSize(width: 2 * scrollView.frame.width, height: scrollView.frame.height)

    scrollView.addSubview(viewController2.view)
    self.addChildViewController(viewController2)
    viewController2.didMoveToParentViewController(self)

    scrollView.addSubview(viewController1.view)
    self.addChildViewController(viewController1)
    viewController1.didMoveToParentViewController(self)

【讨论】:

不使用UIScrollView 代替UIPageViewController 是否还有其他可能的解决方案?或者怎么可能只允许水平滚动? 您的 scrollView 不应垂直滚动,因为您的 contentSize 的高度等于您的 scrollView 的高度。 以你的方式如何检查我现在是否在 viewController2 中? 我没有我的电脑,但你可以像在 containerViewController 中那样使用索引。您可以将 containerViewController 设置为 scrollView 的委托,并在每次内容移动时更改索引。或者您可以使用 parentViewController 属性从 viewDidAppear 中的 UINavigationViewController 更改它。 所以如果我需要再添加 1 个 viewController 我怎样才能让它工作(方程式)?我做宽度:3 * ...?

以上是关于禁用 UIPageViewController 弹跳 - Swift [重复]的主要内容,如果未能解决你的问题,请参考以下文章

UIPageViewController 禁用滚动

仅在 uiPageViewController 中禁用临时向前滑动

禁用屏幕特定区域的 uipageviewcontroller 滑动

禁用 UIPageViewController 弹跳 - Swift [重复]

仅在 UIPageViewController 上的某个方向上禁用用户滑动?

如何在 UIPageViewController 上禁用多点触控?