Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion CardViewAnimation/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class ViewController: UIViewController {
return cardVisible ? .collapsed : .expanded
}

var velocityFactor : CGFloat = 1000

var runningAnimations = [UIViewPropertyAnimator]()
var animationProgressWhenInterrupted:CGFloat = 0

Expand Down Expand Up @@ -78,7 +80,29 @@ class ViewController: UIViewController {
fractionComplete = cardVisible ? fractionComplete : -fractionComplete
updateInteractiveTransition(fractionCompleted: fractionComplete)
case .ended:
continueInteractiveTransition()
//Here, check where to snap the card view as velocity and position
//of card view (while drag) changes.
//velocityFactor decides how much velocity contributes in the
//decision of where to snap the card view.
let translation = recognizer.translation(in: self.cardViewController.handleArea)
var fractionCompleted = translation.y/cardHeight
var velocity = recognizer.velocity(in: self.cardViewController.handleArea).y
velocity = velocity > 0 ? velocity : -velocity
fractionCompleted = fractionCompleted > 0 ? fractionCompleted : -fractionCompleted
let modifiedFraction = velocity/velocityFactor + fractionCompleted
if modifiedFraction >= 0.5 {
//Just go with the flow
continueInteractiveTransition()
} else {
//Reverse animation direction
for animator in runningAnimations {
animator.isReversed = true
}
//Change card state to cancel out the one in the completion block.
cardVisible = !cardVisible
continueInteractiveTransition()
}

default:
break
}
Expand Down