Skip to content

Commit

Permalink
feat: configure maximum of vertical distance to recognize a gesture
Browse files Browse the repository at this point in the history
  • Loading branch information
lengocduy committed Nov 9, 2021
1 parent 7622822 commit 10ba6fe
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 11 deletions.
4 changes: 3 additions & 1 deletion DLSwipeToPopController/Impl/ SwipeToPopTableViewController.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import UIKit
// MARK: - SwipeToPopTableViewController

open class SwipeToPopTableViewController: UITableViewController, SwipeToPopControllable {
public var isDragging = false

public var percentDrivenInteractiveTransition: UIPercentDrivenInteractiveTransition?
// swiftlint:disable implicitly_unwrapped_optional
public var panGestureRecognizer: UIPanGestureRecognizer!
Expand All @@ -33,7 +35,7 @@ open class SwipeToPopTableViewController: UITableViewController, SwipeToPopContr

/// Default percent 0.5 and horizontal velocity 1000
open var swipeToPopConfig: SwipeToPopConfig {
SwipeToPopConfig(percent: 0.5, velocity: 1000)
SwipeToPopConfig(percent: 0.5, velocity: 1000, maximumOfVerticalDistance: 30)
}
}

Expand Down
3 changes: 2 additions & 1 deletion DLSwipeToPopController/Impl/ SwipeToPopViewController.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import UIKit
// MARK: - SwipeToPopViewController

open class SwipeToPopViewController: UIViewController, SwipeToPopControllable {
public var isDragging = false
public var percentDrivenInteractiveTransition: UIPercentDrivenInteractiveTransition?
// swiftlint:disable implicitly_unwrapped_optional
public var panGestureRecognizer: UIPanGestureRecognizer!
Expand All @@ -33,7 +34,7 @@ open class SwipeToPopViewController: UIViewController, SwipeToPopControllable {

/// Default percent 0.5 and horizontal velocity 1000
open var swipeToPopConfig: SwipeToPopConfig {
SwipeToPopConfig(percent: 0.5, velocity: 1000)
SwipeToPopConfig(percent: 0.5, velocity: 1000, maximumOfVerticalDistance: 30)
}
}

Expand Down
7 changes: 6 additions & 1 deletion DLSwipeToPopController/Model/SwipeToPopConfig.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ public struct SwipeToPopConfig {

/// Velocity for swipe even in horizontal direction
let velocity: CGFloat

/// Maximum of Vertical distance to recognize gesture.
/// If the distance larger than this property's value. Skip handling due to considering it is not a swipe right gesture
let maximumOfVerticalDistance: CGFloat

public init(percent: CGFloat, velocity: CGFloat) {
public init(percent: CGFloat, velocity: CGFloat, maximumOfVerticalDistance: CGFloat) {
self.percent = percent
self.velocity = velocity
self.maximumOfVerticalDistance = maximumOfVerticalDistance
}
}
19 changes: 16 additions & 3 deletions DLSwipeToPopController/Protocol/SwipeToPopControllable.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

import UIKit

public protocol SwipeToPopControllable: AnyObject, UINavigationControllerDelegate, UIGestureRecognizerDelegate {
public protocol SwipeToPopControllable: UINavigationControllerDelegate, UIGestureRecognizerDelegate {
var percentDrivenInteractiveTransition: UIPercentDrivenInteractiveTransition? { get set }

// swiftlint:disable implicitly_unwrapped_optional
var panGestureRecognizer: UIPanGestureRecognizer! { get set }

/// Customize for handle even once user end his action panGesture.state == .ended : drag or swipe
var swipeToPopConfig: SwipeToPopConfig { get }

/// Check whether user is dragging or not. If User is dragging just let user continue with the action
var isDragging: Bool { get set }

/// Callback when ViewController was popped
func didPopViewController()
Expand Down Expand Up @@ -85,19 +88,29 @@ public extension SwipeToPopControllable where Self: UIViewController {
switch panGesture.state {

case .began:
isDragging = false
navigationController?.delegate = self
_ = navigationController?.popViewController(animated: true)

case .changed:
let translation = panGesture.translation(in: view)
let shouldRecognize = abs(translation.x) > abs(translation.y) && abs(translation.y) < swipeToPopConfig.maximumOfVerticalDistance || isDragging

if !shouldRecognize {
percentDrivenInteractiveTransition?.cancel()
break
}

isDragging = true
if let percentDrivenInteractiveTransition = percentDrivenInteractiveTransition {
percentDrivenInteractiveTransition.update(percent)
}

case .ended:
let velocity = panGesture.velocity(in: view).x
let velocity = panGesture.velocity(in: view)

// Continue if drag more than customized percent of screen width or velocity is higher than customized velocity
if percent > swipeToPopConfig.percent || velocity > swipeToPopConfig.velocity {
if percent > swipeToPopConfig.percent || velocity.x > swipeToPopConfig.velocity {
percentDrivenInteractiveTransition?.finish()
didPopViewController()
} else {
Expand Down
4 changes: 3 additions & 1 deletion Example/DLSwipeToPopController/SwipeTableViewController.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class SwipeTableViewController: SwipeToPopTableViewController {

override func viewDidLoad() {
super.viewDidLoad()

tableView.backgroundColor = .white
}

// MARK: - Table view data source
Expand All @@ -29,6 +31,6 @@ class SwipeTableViewController: SwipeToPopTableViewController {

/// Default percent 0.25 and horizontal velocity 100 for pan's state is ended to continue the action
override var swipeToPopConfig: SwipeToPopConfig {
return SwipeToPopConfig(percent: 0.1, velocity: 500)
return SwipeToPopConfig(percent: 0.1, velocity: 500, maximumOfVerticalDistance: 20)
}
}
2 changes: 1 addition & 1 deletion Example/DLSwipeToPopController/SwipeViewController.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class SwipeViewController: SwipeToPopViewController {

/// Default percent 0.25 and horizontal velocity 100 for pan's state is ended to continue the action
override var swipeToPopConfig: SwipeToPopConfig {
return SwipeToPopConfig(percent: 0.25, velocity: 100)
return SwipeToPopConfig(percent: 0.25, velocity: 100, maximumOfVerticalDistance: 30.0)
}
}
2 changes: 1 addition & 1 deletion Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
DLSwipeToPopController: 99888bf6b5cdfbf5b1ea2d3a824225ccc3e8b42e
DLSwipeToPopController: b032c67916d75839fa0e49d1ab351f55d24c8c16

PODFILE CHECKSUM: 5c76193d145644460e7ede81abd70ef8096b27f5

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 10ba6fe

Please sign in to comment.