Last active
March 25, 2020 10:32
-
-
Save IamMarik/ce6a7b9845aa26f881a5cb3d07deab36 to your computer and use it in GitHub Desktop.
#transition #swift
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
// | |
// MenuViewController.swift | |
// Mullt | |
// | |
// Created by Marik on 09.03.2020. | |
// Copyright © 2020 Marik. All rights reserved. | |
// | |
import UIKit | |
class MenuViewController: UIViewController { | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
modalPresentationStyle = .custom | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
@IBAction func toModule1(_ sender: Any) { | |
if let menuController = self.navigationController as? MenuNavigationController { | |
let controller = UIStoryboard(name: "Module1", bundle: nil).instantiateInitialViewController()! as! UINavigationController | |
menuController.presentNavigationController(controller, animated: false) | |
} | |
} | |
@IBAction func toModule1Sub(_ sender: Any) { | |
if let menuController = self.navigationController as? MenuNavigationController { | |
let controller = UIStoryboard(name: "Module1", bundle: nil).instantiateInitialViewController()! as! UINavigationController | |
menuController.presentNavigationController(controller, animated: true) | |
} | |
} | |
} | |
class MenuNavigationController: UINavigationController { | |
var customTransitionDelegate = TransitionDelegate() | |
weak var presentedController: UINavigationController? | |
var interactiveTransition: UIPercentDrivenInteractiveTransition? | |
func presentNavigationController(_ navController: UINavigationController, animated: Bool) { | |
navController.modalPresentationStyle = .custom | |
navController.transitioningDelegate = self.customTransitionDelegate | |
if let firstController = navController.viewControllers.first { | |
let panDown = UIPanGestureRecognizer(target: self, action: #selector(handleDissmiss(_:))) | |
firstController.view.addGestureRecognizer(panDown) | |
} | |
self.present(navController, animated: animated) { | |
self.presentedController = navController | |
} | |
} | |
@objc func handleDissmiss(_ gesture: UIPanGestureRecognizer) { | |
let translate = gesture.translation(in: gesture.view) | |
let percent = translate.x / gesture.view!.bounds.size.width | |
if gesture.state == .began { | |
if gesture.location(in: gesture.view).x / gesture.view!.bounds.size.width < 0.3 { | |
interactiveTransition = UIPercentDrivenInteractiveTransition() | |
customTransitionDelegate.interactionController = interactiveTransition | |
self.dismiss(animated: true, completion: nil) | |
} | |
} else if gesture.state == .changed { | |
interactiveTransition?.update(percent) | |
} else if gesture.state == .ended || gesture.state == .cancelled { | |
let velocity = gesture.velocity(in: gesture.view) | |
if percent > 0.5 || velocity.x > 500 { | |
interactiveTransition?.finish() | |
} else { | |
interactiveTransition?.cancel() | |
} | |
interactiveTransition = nil | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment