如何实现水平滚动/分页视图控制器?

时间:2015-05-18 08:56:31

标签: ios swift

正如您在我附带的屏幕截图中看到的那样。屏幕上有2个按钮:视频和朋友。

每当用户触摸按钮Videos时,以下视图将向左滑动以显示视频内容。当用户触摸按钮Friends时,它将向右滑动以显示朋友列表。

我在http://sandmoose.com/post/35714028270/storyboards-with-custom-container-view-controllers找到了关于Objective-C的教程。我一步一步地跟着,总是收到这样的错误:

'Application tried to present modally an active controller <KaptrProject.ProfileViewController: 0x7ffcb2825910>.'

我的项目基于Swift,我认为Swift不允许使用transitionFromViewController()更改视图。

请看一下并帮我解决这个问题。任何建议或接近也是受欢迎的。

顺便说一下,这是我在教程之后从Obj-c转换的快速代码:

  //
//  ContainerViewController.swift
//  KaptrProject
//
//  Created by Binh Nguyen on 5/14/15.
//  Copyright (c) 2015 TrangNguyen. All rights reserved.
//

import UIKit

class ContainerViewController: UIViewController {
    let SegueIdentifierFirst = "embedFirst"
    let SegueIdentifierSecond = "embedSecond"
    var currentSegue = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        currentSegue = SegueIdentifierFirst;
        performSegueWithIdentifier(currentSegue, sender: nil)
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        var destVC = segue.destinationViewController as UIViewController

        if segue.identifier == SegueIdentifierFirst {
            if self.childViewControllers.count > 0{
                var currVC = self.childViewControllers.first as UIViewController
                swap(fromVC: currVC as UIViewController, toVC: destVC)
            }else{
                self.addChildViewController(destVC)
                destVC.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)

                self.view.addSubview(destVC.view)
                destVC.didMoveToParentViewController(self)
            }
        }else if segue.identifier == SegueIdentifierSecond{
            var currVC = self.childViewControllers.first as UIViewController
            swap(fromVC: currVC, toVC: destVC)
        }

    }

    func swap(#fromVC:UIViewController, toVC :UIViewController){

        toVC.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height )
        fromVC.willMoveToParentViewController(nil)

        addChildViewController(toVC)

        transitionFromViewController(fromVC, toViewController: toVC, duration: 1.0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: nil, completion:  { finished in
            fromVC.removeFromParentViewController()     
            toVC.didMoveToParentViewController(self)
        })
    }

    func swapVC(){
        println("swap!!!!!!!!!!!!!!!!!!!!!")
        if (currentSegue == SegueIdentifierFirst){
            currentSegue = SegueIdentifierSecond
        }else{
            currentSegue = SegueIdentifierFirst
        }
        performSegueWithIdentifier(currentSegue, sender: nil)

    }

}

enter image description here

2 个答案:

答案 0 :(得分:1)

为什么不使用UIPageViewController?它将为您提供您正在寻找的内容,以及其他一些页面过渡动画。您可以找到一个可以开始的好教程here

答案 1 :(得分:0)

尝试使用容器视图(子视图)。 相应地添加和删除子视图控制器