From 8e2f6fe2f1eb8aa5122f303dd676b33cfcc21406 Mon Sep 17 00:00:00 2001 From: Badr Ibrahim Date: Sun, 30 Dec 2018 18:26:48 +0100 Subject: [PATCH 1/2] [Feature]: Snap the card view to top or bottom according to velocity of the pan gesture and to what extend the card has been dragged. --- CardViewAnimation.xcodeproj/project.pbxproj | 4 +-- CardViewAnimation/ViewController.swift | 27 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/CardViewAnimation.xcodeproj/project.pbxproj b/CardViewAnimation.xcodeproj/project.pbxproj index a01ab08..d0c30a3 100644 --- a/CardViewAnimation.xcodeproj/project.pbxproj +++ b/CardViewAnimation.xcodeproj/project.pbxproj @@ -291,7 +291,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = ""; + DEVELOPMENT_TEAM = P55Z59EHDR; INFOPLIST_FILE = CardViewAnimation/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", @@ -309,7 +309,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = ""; + DEVELOPMENT_TEAM = P55Z59EHDR; INFOPLIST_FILE = CardViewAnimation/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", diff --git a/CardViewAnimation/ViewController.swift b/CardViewAnimation/ViewController.swift index 1615c44..b79a1db 100644 --- a/CardViewAnimation/ViewController.swift +++ b/CardViewAnimation/ViewController.swift @@ -27,6 +27,8 @@ class ViewController: UIViewController { return cardVisible ? .collapsed : .expanded } + var velocityFactor : CGFloat = 1000 + var runningAnimations = [UIViewPropertyAnimator]() var animationProgressWhenInterrupted:CGFloat = 0 @@ -78,7 +80,30 @@ 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.stopAnimation(true) + } + runningAnimations.removeAll() + cardVisible = !cardVisible + animateTransitionIfNeeded(state: nextState, duration: 0.9) + } + default: break } From 2104516ae58f9fabad7b3d8c328e85c5d949150a Mon Sep 17 00:00:00 2001 From: Badr Ibrahim Date: Mon, 31 Dec 2018 01:36:59 +0100 Subject: [PATCH 2/2] Used "isReversed" property instead of handling the reverse animation manually. --- CardViewAnimation.xcodeproj/project.pbxproj | 4 ++-- CardViewAnimation/ViewController.swift | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/CardViewAnimation.xcodeproj/project.pbxproj b/CardViewAnimation.xcodeproj/project.pbxproj index d0c30a3..a01ab08 100644 --- a/CardViewAnimation.xcodeproj/project.pbxproj +++ b/CardViewAnimation.xcodeproj/project.pbxproj @@ -291,7 +291,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = P55Z59EHDR; + DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = CardViewAnimation/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", @@ -309,7 +309,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = P55Z59EHDR; + DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = CardViewAnimation/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", diff --git a/CardViewAnimation/ViewController.swift b/CardViewAnimation/ViewController.swift index b79a1db..c445184 100644 --- a/CardViewAnimation/ViewController.swift +++ b/CardViewAnimation/ViewController.swift @@ -80,7 +80,6 @@ class ViewController: UIViewController { fractionComplete = cardVisible ? fractionComplete : -fractionComplete updateInteractiveTransition(fractionCompleted: fractionComplete) case .ended: - //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 @@ -97,11 +96,11 @@ class ViewController: UIViewController { } else { //Reverse animation direction for animator in runningAnimations { - animator.stopAnimation(true) + animator.isReversed = true } - runningAnimations.removeAll() + //Change card state to cancel out the one in the completion block. cardVisible = !cardVisible - animateTransitionIfNeeded(state: nextState, duration: 0.9) + continueInteractiveTransition() } default: