Created
July 17, 2017 08:05
-
-
Save drinkius/f33dcdafb6fe72d7a0205788fe9fb21e to your computer and use it in GitHub Desktop.
Left/right segues, Swift 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIStoryboardSegue { | |
func presentWith(direction: SegueTransitionDirection) { | |
let offset: CGFloat | |
switch direction { | |
case .right: | |
offset = -1 | |
case .left: | |
offset = 1 | |
} | |
let firstClassView = self.source.view | |
let secondClassView = self.destination.view | |
secondClassView?.isUserInteractionEnabled = false | |
let screenWidth = UIScreen.main.bounds.size.width | |
let screenHeight = UIScreen.main.bounds.size.height | |
secondClassView?.frame = CGRect(x: -1 * offset * screenWidth, y: 0, width: screenWidth, height: screenHeight) | |
if let window = UIApplication.shared.keyWindow { | |
window.insertSubview(secondClassView!, aboveSubview: firstClassView!) | |
UIView.animate(withDuration: 0.2, animations: { () -> Void in | |
firstClassView?.frame = (firstClassView?.frame.offsetBy(dx: offset * screenWidth, dy: 0))! | |
secondClassView?.frame = (secondClassView?.frame.offsetBy(dx: offset * screenWidth, dy: 0))! | |
}, completion: {(Finished) -> Void in | |
self.source.navigationController?.present(self.destination, | |
animated: false, | |
completion: { _ in return | |
secondClassView?.isUserInteractionEnabled = true | |
}) | |
}) | |
} | |
} | |
func unwindWith(direction: SegueTransitionDirection) { | |
let sourceView = self.source.view as UIView! | |
let destinationView = self.destination.view as UIView! | |
let window = UIApplication.shared.delegate?.window! | |
// window?.insertSubview(destinationView!, belowSubview: sourceView!) | |
destinationView?.center = CGPoint(x: (sourceView?.center.x)!, y: (sourceView?.center.y)! + (destinationView?.center.y)!) | |
UIView.animate(withDuration: 0.4, | |
animations: { | |
destinationView?.center = CGPoint(x: (sourceView?.center.x)!, y: (sourceView?.center.y)!) | |
sourceView?.center = CGPoint(x: (sourceView?.center.x)!, y: 0 - 2 * (destinationView?.center.y)!) | |
}, completion: { | |
(value: Bool) in | |
//4. dismiss | |
let sourceNavCont = self.source.navigationController?.viewControllers | |
var strings1: [String] = [] | |
for vc in sourceNavCont! { | |
strings1.append(String(describing: vc)) | |
} | |
let destNavCont = self.destination.navigationController?.viewControllers | |
var strings2: [String] = [] | |
for vc in destNavCont! { | |
strings2.append(String(describing: vc)) | |
} | |
dump(strings1) | |
dump(strings2) | |
self.source.navigationController?.view.removeFromSuperview() | |
if let navController = self.destination.navigationController { | |
let appDelegate = UIApplication.shared.delegate as! AppDelegate | |
appDelegate.window?.rootViewController = navController | |
} | |
}) | |
} | |
} | |
enum SegueTransitionDirection { | |
case left | |
case right | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment