使用UIPageViewController将PageCurl转换为Scroll

时间:2015-01-18 21:11:50

标签: ios swift xcode6 uipageviewcontroller swift-playground

我想在视图之间更改动画。 目前我有PageCurl但我想拥有像Snapchat这样的Scroll动画。

我无法使用var transitionStyle: UIPageViewControllerTransitionStyle { get }

enter image description here

我的ViewController:

import UIKit

class ViewController: UIPageViewController, UIPageViewControllerDataSource{

    let pageViewController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
    var messagesViewController : MessagesViewController!
    var searchViewController : SearchViewController!
    var friendsViewController : FriendsViewController!

    override func viewDidLoad() {
        super.viewDidLoad()

        // set UIPageViewControllerDataSource
        self.dataSource = self

        // Reference all of the view controllers on the storyboard
        self.messagesViewController = self.storyboard?.instantiateViewControllerWithIdentifier("messagesViewController") as? MessagesViewController
        self.messagesViewController.title = "Messages"

        self.searchViewController = self.storyboard?.instantiateViewControllerWithIdentifier("searchViewController") as? SearchViewController
        self.searchViewController.title = "Search"

        self.friendsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("friendsViewController") as? FriendsViewController
        self.friendsViewController.title = "Friends"

        // Set starting view controllers
        var startingViewControllers : NSArray = [self.searchViewController]
        self.setViewControllers(startingViewControllers, direction: .Forward, animated: false, completion: nil)
    }


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

        switch viewController.title! {
        case "Messages":
            return nil
        case "Search":
            return messagesViewController
        case "Friends":
            return searchViewController
        default:
            return nil
        }
    }

    // pink green blue

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

        switch viewController.title! {
        case "Messages":
            return searchViewController
        case "Search":
            return friendsViewController
        case "Friends":
            return nil
        default:
            return nil

        }
    }

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

}

2 个答案:

答案 0 :(得分:10)

来自Apple Docs

“初始化页面视图控制器时设置此属性的值,并且无法更改。”

初始化时必须指定过渡样式。如果您在代码中执行此操作,则需要以下内容:

let pageViewController = ViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)

(因为ViewControllerUIPageViewController

的子类

如果要在故事板中设置页面视图控制器,请在属性检查器中修改过渡样式:

Image of attributes inspector

答案 1 :(得分:2)

如果您需要使用storyboard并且仍需要在代码中设置过渡样式,则可以在继承UIPageViewController的类中使用此技巧。诀窍在于使用编码器在init中调用super.init(),它采用过渡样式,如此

class ViewController: UIPageViewController {

    required init?(coder: NSCoder) {
        super.init(transitionStyle: Configs.transitionStyle, navigationOrientation: .horizontal, options: nil)
    }
}