Commit 41f34e42 by Daniel Dahan

development: adding presentations to MotionTransition

parent 9ee829f1
...@@ -58,7 +58,7 @@ extension UIViewController { ...@@ -58,7 +58,7 @@ extension UIViewController {
} }
} }
open class CollectionViewController: UIViewController { open class CollectionViewController: MotionTransitionViewController {
/// A reference to a Reminder. /// A reference to a Reminder.
open let collectionView = CollectionView() open let collectionView = CollectionView()
...@@ -81,7 +81,8 @@ open class CollectionViewController: UIViewController { ...@@ -81,7 +81,8 @@ open class CollectionViewController: UIViewController {
The super.prepareView method should always be called immediately The super.prepareView method should always be called immediately
when subclassing. when subclassing.
*/ */
open func prepare() { open override func prepare() {
super.prepare()
view.clipsToBounds = true view.clipsToBounds = true
view.backgroundColor = .white view.backgroundColor = .white
view.contentScaleFactor = Screen.scale view.contentScaleFactor = Screen.scale
......
...@@ -43,10 +43,6 @@ fileprivate struct MotionTransitionItemController { ...@@ -43,10 +43,6 @@ fileprivate struct MotionTransitionItemController {
} }
extension UIViewController { extension UIViewController {
// override func transition(from fromViewController: UIViewController, to toViewController: UIViewController, duration: TimeInterval, options: UIViewAnimationOptions = [], animations: (() -> Void)?, completion: ((Bool) -> Void)? = nil) {
//
// }
/// MaterialLayer Reference. /// MaterialLayer Reference.
fileprivate var motionTransition: MotionTransitionItemController { fileprivate var motionTransition: MotionTransitionItemController {
get { get {
...@@ -64,6 +60,34 @@ extension UIViewController { ...@@ -64,6 +60,34 @@ extension UIViewController {
} }
} }
open class MotionTransitionViewController: UIViewController {
public init() {
super.init(nibName: nil, bundle: nil)
prepare()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
prepare()
}
public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
prepare()
}
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepare method
to initialize property values and other setup operations.
The super.prepare method should always be called immediately
when subclassing.
*/
open func prepare() {
transitioningDelegate = transitionDelegate
}
}
extension UIView { extension UIView {
/// MaterialLayer Reference. /// MaterialLayer Reference.
fileprivate var motionTransition: MotionTransitionItem { fileprivate var motionTransition: MotionTransitionItem {
...@@ -96,23 +120,20 @@ extension UIView { ...@@ -96,23 +120,20 @@ extension UIView {
} }
} }
open class MotionTransitionAnimator: NSObject, UIViewControllerAnimatedTransitioning { open class MotionTransitionPresentationController: UIPresentationController {
public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { open override func presentationTransitionWillBegin() {
guard let containerView = containerView else {
} return
public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.25
} }
}
open class MotionTransitionInteractiveAnimator: NSObject, UIViewControllerInteractiveTransitioning {
public func startInteractiveTransition(_ transitionContext: UIViewControllerContextTransitioning) {
print(presentedViewController)
print("Presented")
} }
} }
open class MotionTransitionDelegate: NSObject { open class MotionTransitionDelegate: NSObject {
open var isPresenting = false open var isPresenting = false
open var transitionContext: UIViewControllerContextTransitioning!
open var containerView: UIView! open var containerView: UIView!
...@@ -125,6 +146,7 @@ open class MotionTransitionDelegate: NSObject { ...@@ -125,6 +146,7 @@ open class MotionTransitionDelegate: NSObject {
open var fromViewController: UIViewController! open var fromViewController: UIViewController!
open var fromViewFinalFrame: CGRect! open var fromViewFinalFrame: CGRect!
@objc(animateTransition:)
open func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { open func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let tView = transitionContext.view(forKey: .to) else { guard let tView = transitionContext.view(forKey: .to) else {
return return
...@@ -141,8 +163,9 @@ open class MotionTransitionDelegate: NSObject { ...@@ -141,8 +163,9 @@ open class MotionTransitionDelegate: NSObject {
guard let fVC = transitionContext.viewController(forKey: .from) else { guard let fVC = transitionContext.viewController(forKey: .from) else {
return return
} }
self.transitionContext = transitionContext
let containerView = transitionContext.containerView containerView = transitionContext.containerView
toView = tView toView = tView
toViewController = tVC toViewController = tVC
...@@ -155,9 +178,14 @@ open class MotionTransitionDelegate: NSObject { ...@@ -155,9 +178,14 @@ open class MotionTransitionDelegate: NSObject {
fromViewFinalFrame = transitionContext.finalFrame(for: fromViewController) fromViewFinalFrame = transitionContext.finalFrame(for: fromViewController)
} }
@objc(transitionDuration:)
open func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { open func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.25 return 0.25
} }
open func animationEnded(_ transitionCompleted: Bool) {
print("MotionTransitionAnimator", #function)
}
} }
extension MotionTransitionDelegate: UIViewControllerTransitioningDelegate { extension MotionTransitionDelegate: UIViewControllerTransitioningDelegate {
...@@ -176,6 +204,10 @@ extension MotionTransitionDelegate: UIViewControllerTransitioningDelegate { ...@@ -176,6 +204,10 @@ extension MotionTransitionDelegate: UIViewControllerTransitioningDelegate {
open func interactionControllerForPresentation(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? { open func interactionControllerForPresentation(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return MotionTransitionInteractiveAnimator() return MotionTransitionInteractiveAnimator()
} }
public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return MotionTransitionPresentationController(presentedViewController: presented, presenting: presenting)
}
} }
extension MotionTransitionDelegate: UINavigationControllerDelegate { extension MotionTransitionDelegate: UINavigationControllerDelegate {
...@@ -190,10 +222,6 @@ extension MotionTransitionDelegate: UINavigationControllerDelegate { ...@@ -190,10 +222,6 @@ extension MotionTransitionDelegate: UINavigationControllerDelegate {
} }
extension MotionTransitionDelegate: UITabBarControllerDelegate { extension MotionTransitionDelegate: UITabBarControllerDelegate {
// open func tabBarController(_ tabBarController: UITabBarController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
// return MotionTransitionAnimator()
// }
open func tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { open func tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
isPresenting = true isPresenting = true
self.fromViewController = fromViewController ?? fromVC self.fromViewController = fromViewController ?? fromVC
...@@ -201,6 +229,114 @@ extension MotionTransitionDelegate: UITabBarControllerDelegate { ...@@ -201,6 +229,114 @@ extension MotionTransitionDelegate: UITabBarControllerDelegate {
// self.inContainerController = true // self.inContainerController = true
return MotionTransitionAnimator() return MotionTransitionAnimator()
} }
open func tabBarController(_ tabBarController: UITabBarController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return MotionTransitionInteractiveAnimator()
}
}
open class MotionTransitionInteractiveDelegate: UIPercentDrivenInteractiveTransition {
open var isPresenting = false
open var transitionContext: UIViewControllerContextTransitioning!
open var containerView: UIView!
open var toView: UIView!
open var toViewController: UIViewController!
open var toViewStartFrame: CGRect!
open var toViewFinalFrame: CGRect!
open var fromView: UIView!
open var fromViewController: UIViewController!
open var fromViewFinalFrame: CGRect!
open var panGesture: UIPanGestureRecognizer!
@objc(startInteractiveTransition:)
open override func startInteractiveTransition(_ transitionContext: UIViewControllerContextTransitioning) {
super.startInteractiveTransition(transitionContext)
guard let tView = transitionContext.view(forKey: .to) else {
return
}
guard let tVC = transitionContext.viewController(forKey: .to) else {
return
}
guard let fView = transitionContext.view(forKey: .from) else {
return
}
guard let fVC = transitionContext.viewController(forKey: .from) else {
return
}
self.transitionContext = transitionContext
containerView = transitionContext.containerView
toView = tView
toViewController = tVC
fromView = fView
fromViewController = fVC
toViewStartFrame = transitionContext.initialFrame(for: toViewController)
toViewFinalFrame = transitionContext.finalFrame(for: toViewController)
fromViewFinalFrame = transitionContext.finalFrame(for: fromViewController)
preparePanGesture()
}
open func animationEnded(_ transitionCompleted: Bool) {
print("MotionTransitionAnimator", #function)
}
}
extension MotionTransitionInteractiveDelegate {
fileprivate func preparePanGesture() {
panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(recognizer:)))
panGesture.maximumNumberOfTouches = 1
containerView.addGestureRecognizer(panGesture)
}
}
extension MotionTransitionInteractiveDelegate {
@objc
fileprivate func handlePanGesture(recognizer: UIPanGestureRecognizer) {
switch recognizer.state {
case .began:
panGesture.setTranslation(.zero, in: containerView)
case .changed:
let translation = panGesture.translation(in: containerView)
/**
Compute how far the gesture recognizer tranveled on the
vertical axis.
*/
let percentageComplete = fabs(translation.y / containerView.bounds.height)
update(percentageComplete)
case .ended:
finish()
containerView.removeGestureRecognizer(panGesture)
default:break
}
}
}
open class MotionTransitionAnimator: MotionTransitionDelegate, UIViewControllerAnimatedTransitioning {
@objc(animateTransition:)
open override func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
super.animateTransition(using: transitionContext)
}
}
open class MotionTransitionInteractiveAnimator: MotionTransitionInteractiveDelegate {
open override func startInteractiveTransition(_ transitionContext: UIViewControllerContextTransitioning) {
super.startInteractiveTransition(transitionContext)
}
} }
open class FadeMotionTransition: NSObject, UIViewControllerAnimatedTransitioning { open class FadeMotionTransition: NSObject, UIViewControllerAnimatedTransitioning {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment