Skip to content

Commit

Permalink
Preventing new segue if animation is currently running
Browse files Browse the repository at this point in the history
  • Loading branch information
Øyvind Hauge committed Jan 23, 2016
1 parent 7bec3d8 commit 71d3798
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions OHCircleSegue/OHCircleSegue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import UIKit

class OHCircleSegue: UIStoryboardSegue {

private let expandDur: CFTimeInterval = 0.35
private let contractDur: CFTimeInterval = 0.15
private static let expandDur: CFTimeInterval = 0.35
private static let contractDur: CFTimeInterval = 0.15
private static let stack = Stack()
private static var isAnimating = false

var circleOrigin: CGPoint
private var shouldUnwind: Bool
Expand All @@ -32,6 +33,10 @@ class OHCircleSegue: UIStoryboardSegue {

override func perform() {

if OHCircleSegue.isAnimating {
return
}

if OHCircleSegue.stack.peek() !== destinationViewController {
OHCircleSegue.stack.push(sourceViewController)
} else {
Expand Down Expand Up @@ -66,7 +71,12 @@ class OHCircleSegue: UIStoryboardSegue {

// MARK: Animation delegate

override func animationDidStart(anim: CAAnimation) {
OHCircleSegue.isAnimating = true
}

override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
OHCircleSegue.isAnimating = false
if !shouldUnwind {
sourceViewController.presentViewController(destinationViewController, animated: false, completion: nil)
} else {
Expand All @@ -82,7 +92,7 @@ class OHCircleSegue: UIStoryboardSegue {
animation.toValue = destinationPath
animation.removedOnCompletion = false
animation.fillMode = kCAFillModeBoth
animation.duration = shouldUnwind ? contractDur : expandDur
animation.duration = shouldUnwind ? OHCircleSegue.contractDur : OHCircleSegue.expandDur
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
animation.delegate = self
return animation
Expand All @@ -103,7 +113,7 @@ class OHCircleSegue: UIStoryboardSegue {

// TODO: Clean up implementation ^

print("h1: \(h1) hyp: \(hyp) rw: \(rw) rh: \(rh) width: \(width) height: \(height)")
//print("h1: \(h1) hyp: \(hyp) rw: \(rw) rh: \(rh) width: \(width) height: \(height)")

// The two circle sizes we will animate to/from
let path1 = UIBezierPath(ovalInRect: CGRectZero).CGPath
Expand Down

0 comments on commit 71d3798

Please sign in to comment.