Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transition PageCurl to Scroll with UIPageViewController

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 }

enter image description here

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

}
like image 343
Pixel Avatar asked Jan 18 '15 21:01

Pixel


2 Answers

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:

Image of attributes inspector

like image 194
pbasdf Avatar answered Dec 11 '22 03:12

pbasdf


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)
    }
}
like image 28
Khaled Annajar Avatar answered Dec 11 '22 05:12

Khaled Annajar