 //
-//  OHCircleSegue.swift
-//  OHCircleSegue
+//  OHOHCircleSegue.swift
+//  OHOHCircleSegue
 //
 //  Created by Øyvind Hauge on 10/01/16.
 //  Copyright © 2016 Øyvind Hauge. All rights reserved.

            return
        }
        
        if OHCircleSegue.stack.peek() !== destinationViewController {
            OHCircleSegue.stack.push(sourceViewController)
        } else {
            OHCircleSegue.stack.pop()
            shouldUnwind = true
        }
        
        let sourceView = sourceViewController.view as UIView!
        let destView = destinationViewController.view as UIView!
        
        // Add source (or destination) controller's view to the main application
        // window depending of if this is a normal or unwind segue
        let window = UIApplication.sharedApplication().keyWindow
        if !shouldUnwind {
            window?.insertSubview(destView, aboveSubview: sourceView)
        } else {
            window?.insertSubview(destView, atIndex:0)
        }
        
        let paths = startAndEndPaths(!shouldUnwind)
        
        // Create circle mask and apply it to the view of the destination controller
        let mask = CAShapeLayer()
        mask.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        mask.position = circleOrigin
        mask.path = paths.start
        (shouldUnwind ? sourceView : destView).layer.mask = mask
        
        // Call method for creating animation and add it to the view's mask
        (shouldUnwind ? sourceView : destView).layer.mask?.addAnimation(scalingAnimation(paths.end), forKey: nil)
    }
    
    // MARK: Animation delegate
    
    func animationDidStart(anim: CAAnimation) {
        OHCircleSegue.isAnimating = true
    }
    
    func animationDidStop(anim: CAAnimation, finished flag: Bool) {
        OHCircleSegue.isAnimating = false
        if !shouldUnwind {
            sourceViewController.presentViewController(destinationViewController, animated: false, completion: nil)
        } else {
            sourceViewController.dismissViewControllerAnimated(false, completion: nil)
        }
    }
    
    // MARK: Helper methods
    
    private func scalingAnimation(destinationPath: CGPathRef) -> CABasicAnimation {
        
        let animation = CABasicAnimation(keyPath: "path")
        animation.toValue = destinationPath
        animation.removedOnCompletion = false
        animation.fillMode = kCAFillModeBoth
        animation.duration = shouldUnwind ? OHCircleSegue.contractDur : OHCircleSegue.expandDur
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)

        
        // By default, transition starts from the center of the screen,
        // so let's find the center when segue is first initialized
        let centerX = UIScreen.main.bounds.width*0.5
        let centerY = UIScreen.main.bounds.height*0.5
        let centerOfScreen = CGPoint(x:centerX, y:centerY)
        
        // Initialize properties
        circleOrigin = centerOfScreen
