Search code examples
iosxcodesegueswift3uistoryboardsegue

Segue with animation from left to right


I'm trying to implement a custom segue which will bring the viewcontroller with animation from right to left. The problem is that my app crash when I press the button in order to perform the segue to the needed controller. I receive the following error:

Could not create a segue of class '(null)'

How did I implement it? I created a new class and override the perform function with the following code:

import UIKit

class CustomTransition: UIStoryboardSegue {

override func perform()
{
    let src = self.source
    let dst = self.destination

    src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
    dst.view.transform = CGAffineTransform(translationX: -src.view.frame.size.width, y: 0)

    UIView.animate(withDuration: 0.25,
                               delay: 0.0,
                               options: UIViewAnimationOptions.curveEaseInOut,
                               animations: {
                                dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
        },
                               completion: { finished in
                                src.present(dst, animated: false, completion: nil)
        }
    )
}

}

In my storyboard I have an UIButtoon which I tied using a custom segue ( the one generated by my CustomTransition class ). When I run the app and tap on the button, my app crash with the error posted above. I can't understand why is it crashing. Every article on the web that I found is similar to the way i implemented it.


Solution

  • Did you try not to use segues but transitions for this purpose?

    // create transition
    let transition = CATransition()
    transition.duration = 0.3
    // check other types
    transition.type = kCATransitionMoveIn
    // move from left
    transition.subtype = kCATransitionFromLeft
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    navigationController.view.layer.addAnimation(transition, forKey: kCATransition)
    // push controller
    navigationController.pushViewController(controller, animated: false)