Last active
September 21, 2015 06:35
-
-
Save tolpp/3b4efb995344954956bd to your computer and use it in GitHub Desktop.
UIViewControllerAnimatedTransitioning simple usage. Source : http://gist.tolpp.com/2015/09/swift-uiviewcontrolleranimatedtransitio.html
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
import UIKit | |
class FirstViewController: UIViewController { | |
var secondVc : SecondViewController! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
@IBAction func toSecondTapped(sender: UIButton) { | |
let sb = UIStoryboard(name: "Main", bundle: nil) | |
secondVc = sb.instantiateViewControllerWithIdentifier("SecondVC") as! SecondViewController | |
secondVc.transitioningDelegate = self | |
self.presentViewController(secondVc, animated: true, completion: nil) | |
} | |
} | |
extension FirstViewController : UIViewControllerTransitioningDelegate { | |
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { | |
return TransitionAnimator() | |
} | |
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { | |
return TransitionAnimator() | |
} | |
} | |
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
import UIKit | |
class SecondViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.modalPresentationStyle = .Custom | |
} | |
@IBAction func toFirstTapped(sender: UIButton) { | |
self.dismissViewControllerAnimated(true, completion: nil) | |
} | |
} |
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
// Little Updated version of | |
// http://www.raywenderlich.com/86521/how-to-make-a-view-controller-transition-animation-like-in-the-ping-app | |
import UIKit | |
class TransitionAnimator : NSObject, UIViewControllerAnimatedTransitioning { | |
weak var transitionContext: UIViewControllerContextTransitioning? | |
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval { | |
return 0.5 | |
} | |
func animateTransition(transitionContext: UIViewControllerContextTransitioning) { | |
//1 | |
self.transitionContext = transitionContext | |
//2 | |
let containerView = transitionContext.containerView() | |
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as UIViewController! | |
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as UIViewController! | |
//3 | |
containerView!.addSubview(toViewController.view) | |
//4 | |
let circleMaskPathInitial = UIBezierPath(ovalInRect: CGRect()) | |
let extremePoint = CGPoint(x: fromViewController.view.center.x - 0, y: fromViewController.view.center.y - CGRectGetHeight(toViewController.view.bounds)) | |
let radius = sqrt((extremePoint.x*extremePoint.x) + (extremePoint.y*extremePoint.y)) | |
let circleMaskPathFinal = UIBezierPath(ovalInRect: CGRectInset(fromViewController.view.frame, -radius, -radius)) | |
//5 | |
let maskLayer = CAShapeLayer() | |
maskLayer.path = circleMaskPathFinal.CGPath | |
toViewController.view.layer.mask = maskLayer | |
//6 | |
let maskLayerAnimation = CABasicAnimation(keyPath: "path") | |
maskLayerAnimation.fromValue = circleMaskPathInitial.CGPath | |
maskLayerAnimation.toValue = circleMaskPathFinal.CGPath | |
maskLayerAnimation.duration = self.transitionDuration(transitionContext) | |
maskLayerAnimation.delegate = self | |
maskLayer.addAnimation(maskLayerAnimation, forKey: "path") | |
} | |
override func animationDidStop(anim: CAAnimation, finished flag: Bool) { | |
self.transitionContext?.completeTransition(!self.transitionContext!.transitionWasCancelled()) | |
self.transitionContext?.viewControllerForKey(UITransitionContextFromViewControllerKey)?.view.layer.mask = nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment