Skip to content

Commit

Permalink
Merge pull request #256 from sjoness/feature/vary-card-speed
Browse files Browse the repository at this point in the history
Allow management of the swipe animation duration
  • Loading branch information
Vodolazkyi authored Apr 17, 2017
2 parents 6aae65f + 599e1de commit 52cf039
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
15 changes: 14 additions & 1 deletion Pod/Classes/KolodaView/DraggableCardView/DraggableCardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
import UIKit
import pop

public enum DragSpeed: TimeInterval {
case slow = 2.0
case moderate = 1.5
case `default` = 0.8
case fast = 0.4
}

protocol DraggableCardDelegate: class {

func card(_ card: DraggableCardView, wasDraggedWithFinishPercentage percentage: CGFloat, inDirection direction: SwipeResultDirection)
Expand All @@ -19,13 +26,13 @@ protocol DraggableCardDelegate: class {
func card(cardSwipeThresholdRatioMargin card: DraggableCardView) -> CGFloat?
func card(cardAllowedDirections card: DraggableCardView) -> [SwipeResultDirection]
func card(cardShouldDrag card: DraggableCardView) -> Bool
func card(cardSwipeSpeed card: DraggableCardView) -> DragSpeed
}

//Drag animation constants
private let rotationMax: CGFloat = 1.0
private let defaultRotationAngle = CGFloat(Double.pi) / 10.0
private let scaleMin: CGFloat = 0.8
public let cardSwipeActionAnimationDuration: TimeInterval = 0.4

private let screenSize = UIScreen.main.bounds.size

Expand All @@ -48,6 +55,8 @@ public class DraggableCardView: UIView, UIGestureRecognizerDelegate {
private var dragBegin = false
private var dragDistance = CGPoint.zero
private var swipePercentageMargin: CGFloat = 0.0
private var cardSwipeActionAnimationDuration: TimeInterval = DragSpeed.default.rawValue


//MARK: Lifecycle
init() {
Expand Down Expand Up @@ -87,6 +96,10 @@ public class DraggableCardView: UIView, UIGestureRecognizerDelegate {
tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(DraggableCardView.tapRecognized(_:)))
tapGestureRecognizer.cancelsTouchesInView = false
addGestureRecognizer(tapGestureRecognizer)

if let delegate = delegate {
cardSwipeActionAnimationDuration = delegate.card(cardSwipeSpeed: self).rawValue
}
}

//MARK: Configurations
Expand Down
8 changes: 6 additions & 2 deletions Pod/Classes/KolodaView/KolodaView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ private let defaultAlphaValueSemiTransparent: CGFloat = 0.7
public protocol KolodaViewDataSource: class {

func kolodaNumberOfCards(_ koloda: KolodaView) -> Int
func kolodaSpeedThatCardShouldDrag(_ koloda: KolodaView) -> DragSpeed
func koloda(_ koloda: KolodaView, viewForCardAt index: Int) -> UIView
func koloda(_ koloda: KolodaView, viewForCardOverlayAt index: Int) -> OverlayView?

}

public extension KolodaViewDataSource {
Expand Down Expand Up @@ -132,7 +132,7 @@ open class KolodaView: UIView, DraggableCardDelegate {
private func setupDeck() {
if let dataSource = dataSource {
countOfCards = dataSource.kolodaNumberOfCards(self)

if countOfCards - currentCardIndex > 0 {
let countOfNeededCards = min(countOfVisibleCards, countOfCards - currentCardIndex)

Expand Down Expand Up @@ -326,6 +326,10 @@ open class KolodaView: UIView, DraggableCardDelegate {
let index = currentCardIndex + visibleIndex
return delegate?.koloda(self, shouldDragCardAt: index) ?? true
}

func card(cardSwipeSpeed card: DraggableCardView) -> DragSpeed {
return dataSource?.kolodaSpeedThatCardShouldDrag(self) ?? DragSpeed.default
}

// MARK: Private
private func clear() {
Expand Down

0 comments on commit 52cf039

Please sign in to comment.