George Sabanov
George Sabanov

Reputation: 189

Custom ViewController Transition like UIPageViewController

I want to navigate tru ViewControllers with animations like in PageViewController if you choose Scroll style. So I want to navigate tru them using left/right swipes. How to do it without UIPageViewController. Is there a way to develop reusable class which could be used as main transition style in my app ?

I appreciate any help

Upvotes: 1

Views: 858

Answers (2)

George Sabanov
George Sabanov

Reputation: 189

Well, I managed to do it using Custom Storyboard Segues Here is the code :

Custom segue :

import UIKit
class CustomScrollPagingSegue: UIStoryboardSegue {
override func perform() {
    let firstVCView = self.sourceViewController.view as UIView!
    let secondVCView = self.destinationViewController.view as UIView!
    let screenWidth = UIScreen.mainScreen().bounds.size.width
    let screenHeight = UIScreen.mainScreen().bounds.size.height
    secondVCView.frame = CGRectMake(screenWidth, 0.0, screenWidth, screenHeight)
    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(secondVCView, aboveSubview: firstVCView)
    UIView.animateWithDuration(0.5, animations: { () -> Void in
        firstVCView.frame = CGRectOffset(firstVCView.frame, -screenWidth, 0.0)
        secondVCView.frame = CGRectOffset(secondVCView.frame, -screenWidth, 0.0)
        }) { (Finished) -> Void in
        self.sourceViewController.presentViewController(self.destinationViewController as UIViewController, animated: false, completion: nil)
    }


   }
}

and Custom Unwind Segue

import UIKit

class CustomScrollPagingSegueUnwind: UIStoryboardSegue {
override func perform() {
    // Assign the source and destination views to local variables.
    let secondVCView = self.sourceViewController.view as UIView!
    let firstVCView = self.destinationViewController.view as UIView!

    let screenWidth = UIScreen.mainScreen().bounds.size.width

    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(firstVCView, aboveSubview: secondVCView)

    // Animate the transition.
    UIView.animateWithDuration(0.5, animations: { () -> Void in
        firstVCView.frame = CGRectOffset(firstVCView.frame, screenWidth, 0.0)
        secondVCView.frame = CGRectOffset(secondVCView.frame, screenWidth, 0.0)

        }) { (Finished) -> Void in

            self.sourceViewController.dismissViewControllerAnimated(false, completion: nil)
    }
  }
}

I think having 100% same result is possible with interactive transitions, but I don't know how to implement them.

Upvotes: 0

InkGolem
InkGolem

Reputation: 2762

If you want the same effect as UIPageViewController you should just use it. That's what it's there for. If you have a real reason for not using it then you can google any of the many tutorials for implementing a container view controller.

Upvotes: 1

Related Questions