Transition PageCurl to Scroll with UIPageViewContr

2019-04-09 00:38发布

问题:

I would like to change my animation between the view. Currently i have the PageCurl but i would like to have a Scroll animation like Snapchat.

I can not use the var transitionStyle: UIPageViewControllerTransitionStyle { get }

My 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.
    }

}

回答1:

From the Apple Docs:

"The value of this property is set when the page view controller is initialized, and cannot be changed."

You must specify the transition style when you initialize. If you do that in code, you will need something like:

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

(Since ViewController is a subclass of UIPageViewController)

If you are setting up the Page View Controller in a storyboard, amend the transition style in the attributes inspector:



回答2:

If you need to use storyboard and still need to set the transition style in code you can use this trick in the Class you made inheriting UIPageViewController. The trick is that in the init with coder you call super.init() which takes transition style like this

class ViewController: UIPageViewController {

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