Last active
August 29, 2015 14:26
-
-
Save jkopelioff/19b0001dc17efd84668e to your computer and use it in GitHub Desktop.
ExpandOutModalTransitioningManager.swift
This file contains hidden or 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
| // | |
| // ExpandOutModalTransitioningManager.swift | |
| // eHow | |
| // | |
| // Created by Joel Kopelioff on 7/9/15. | |
| // Copyright (c) 2015 Demand Media. | |
| // | |
| // Permission is hereby granted, free of charge, to any person obtaining a copy | |
| // of this software and associated documentation files (the "Software"), to deal | |
| // in the Software without restriction, including without limitation the rights | |
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| // copies of the Software, and to permit persons to whom the Software is | |
| // furnished to do so, subject to the following conditions: | |
| // | |
| // The above copyright notice and this permission notice shall be included in | |
| // all copies or substantial portions of the Software. | |
| // | |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| // THE SOFTWARE. | |
| import Foundation | |
| class ExpandOutModalTransitioningManager: NSObject, UIViewControllerTransitioningDelegate { | |
| var presenting:Bool = false | |
| var button:UIButton? | |
| var transitionContext:UIViewControllerContextTransitioning? | |
| // MARK: UIViewControllerTransitioningDelegate | |
| func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { | |
| self.presenting = false | |
| return self | |
| } | |
| func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { | |
| self.presenting = true | |
| return self | |
| } | |
| } | |
| extension ExpandOutModalTransitioningManager: UIViewControllerAnimatedTransitioning | |
| { | |
| // MARK: UIViewControllerAnimatedTransitioning | |
| func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval { | |
| return 0.3 | |
| } | |
| func animateTransition(transitionContext: UIViewControllerContextTransitioning) { | |
| // get reference to our fromView, toView and the container view that we should perform the transition in | |
| self.transitionContext = transitionContext | |
| let container = transitionContext.containerView() | |
| let toView = transitionContext.viewForKey(UITransitionContextToViewKey) | |
| let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey) | |
| let button = self.button! | |
| let duration = self.transitionDuration(transitionContext) | |
| container.addSubview(toView!) | |
| if self.presenting { | |
| container.bringSubviewToFront(toView!) | |
| //4 | |
| var circleMaskPathInitial = UIBezierPath(ovalInRect: button.frame) | |
| var extremePoint = CGPoint(x: button.center.x - 0, y: button.center.y - toView!.bounds.height) | |
| var radius = toView!.bounds.height//sqrt((extremePoint.x*extremePoint.x) + (extremePoint.y*extremePoint.y)) | |
| var circleMaskPathFinal = UIBezierPath(ovalInRect: CGRectInset(button.frame, -radius, -radius)) | |
| //5 | |
| var maskLayer = CAShapeLayer() | |
| maskLayer.path = circleMaskPathFinal.CGPath | |
| toView!.layer.mask = maskLayer | |
| //6 | |
| var maskLayerAnimation = CABasicAnimation(keyPath: "path") | |
| maskLayerAnimation.fromValue = circleMaskPathInitial.CGPath | |
| maskLayerAnimation.toValue = circleMaskPathFinal.CGPath | |
| maskLayerAnimation.duration = duration | |
| maskLayerAnimation.delegate = self | |
| maskLayer.addAnimation(maskLayerAnimation, forKey: "path") | |
| } | |
| else { | |
| container.bringSubviewToFront(fromView!) | |
| //4 | |
| var circleMaskPathInitial = UIBezierPath(ovalInRect: button.frame) | |
| var extremePoint = CGPoint(x: button.center.x - 0, y: button.center.y) | |
| var radius = sqrt((extremePoint.x*extremePoint.x) + (extremePoint.y*extremePoint.y)) | |
| var circleMaskPathFinal = UIBezierPath(ovalInRect: CGRectInset(button.frame, -radius, -radius)) | |
| //5 | |
| var maskLayer = CAShapeLayer() | |
| maskLayer.path = circleMaskPathFinal.CGPath | |
| fromView!.layer.mask = maskLayer | |
| //6 | |
| var maskLayerAnimation = CABasicAnimation(keyPath: "path") | |
| maskLayerAnimation.fromValue = circleMaskPathFinal.CGPath | |
| maskLayerAnimation.toValue = circleMaskPathInitial.CGPath | |
| maskLayerAnimation.duration = duration | |
| maskLayerAnimation.delegate = self | |
| maskLayer.addAnimation(maskLayerAnimation, forKey: "path") | |
| } | |
| } | |
| override func animationDidStop(anim: CAAnimation!, finished flag: Bool) { | |
| self.transitionContext?.completeTransition(!self.transitionContext!.transitionWasCancelled()) | |
| self.transitionContext?.viewForKey(UITransitionContextFromViewKey)?.removeFromSuperview() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment