Skip to content

Commit

Permalink
fix swipe logic and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
horitaku46 committed Mar 20, 2018
1 parent 82958b3 commit f8621ac
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 21 deletions.
4 changes: 3 additions & 1 deletion BlowinSwiper/BlowinSwipeable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public protocol BlowinSwipeable: class {
public extension BlowinSwipeable where Self: UIViewController {

public func configureSwipeBack(isLowSensitivity: Bool = false) {
guard let navigationController = self.navigationController else { return }
guard let navigationController = navigationController else { return }
blowinSwiper = BlowinSwiper(navigationController: navigationController)
blowinSwiper?.isLowSensitivity = isLowSensitivity
navigationController.delegate = blowinSwiper
Expand All @@ -28,6 +28,8 @@ public extension BlowinSwipeable where Self: UIViewController {
guard let scrollView = scrollView else { return }
if scrollView.contentOffset.x == 0 {
blowinSwiper?.isShouldRecognizeSimultaneously = true
} else {
blowinSwiper?.isShouldRecognizeSimultaneously = false
}
}

Expand Down
26 changes: 17 additions & 9 deletions BlowinSwiper/BlowinSwiper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class BlowinSwiper: NSObject {
private var navigationController: UINavigationController?
private var percentDriven = UIPercentDrivenInteractiveTransition()
private var isInteractivePop = false
private var isDecideBack = false

public init(navigationController: UINavigationController?) {
super.init()
Expand All @@ -39,6 +40,9 @@ public final class BlowinSwiper: NSObject {
}

@objc private func handlePanGesture(_ gesture: UIPanGestureRecognizer) {
// order not to the extra swipe during transition.
if isDecideBack { return }

guard let view = gesture.view else {
return
}
Expand All @@ -59,10 +63,13 @@ public final class BlowinSwiper: NSObject {
let percent = translation.x > 0 ? translation.x / view.bounds.width : 0
percentDriven.update(percent)

case .ended, .cancelled:
isInteractivePop = false
let maxWidth = view.bounds.width / 3
velocity.x > Const.maxSwipeVelocityX || translation.x > maxWidth ? percentDriven.finish() : percentDriven.cancel()
case .ended, .failed, .cancelled:
if isInteractivePop {
isInteractivePop = false
let maxWidth = view.bounds.width / 3
isDecideBack = velocity.x > Const.maxSwipeVelocityX || translation.x > maxWidth
isDecideBack ? percentDriven.finish() : percentDriven.cancel()
}

default:
break
Expand All @@ -76,17 +83,18 @@ extension BlowinSwiper: UIGestureRecognizerDelegate {
return navigationController?.viewControllers.count ?? 0 > 1
}

public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return isShouldRecognizeSimultaneously
}
}

extension BlowinSwiper: UINavigationControllerDelegate {

public func navigationController(_ navigationController: UINavigationController,
animationControllerFor operation: UINavigationControllerOperation,
from fromVC: UIViewController,
to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
animationControllerFor operation: UINavigationControllerOperation,
from fromVC: UIViewController,
to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
switch operation {
case .pop:
return PopAnimatedTransitioning(isInteractivePop: isInteractivePop)
Expand All @@ -97,7 +105,7 @@ extension BlowinSwiper: UINavigationControllerDelegate {
}

public func navigationController(_ navigationController: UINavigationController,
interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return isInteractivePop ? percentDriven : nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ final class ColorViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = backgroundColor
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

navigationItem.titleView = UILabel.navigationItemTitle("FIrst")
navigationItem.titleView = UILabel.navigationItemTitle("First")
view.backgroundColor = UIColor(hex: ColorHex.green)

let rightShowBarButtonItem = UIBarButtonItem(title: "Show",
Expand Down
7 changes: 3 additions & 4 deletions Example/Sources/Controllers/Menu/MenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,29 @@ final class MenuViewController: UIViewController, BlowinSwipeable {

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

// Enable scrolling by putting finger back during swipe back
swipeMenuView.contentScrollView?.isScrollEnabled = true
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
configureSwipeBack(isLowSensitivity: true)
enabledRecognizeSimultaneously(scrollView: swipeMenuView.contentScrollView)
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

// Stop the scrollView while swiping back
swipeMenuView.contentScrollView?.isScrollEnabled = false
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)

disabledRecognizeSimultaneously()
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

swipeMenuView.willChangeOrientation()
}

Expand Down
6 changes: 2 additions & 4 deletions Example/Sources/Controllers/Second/SecondViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ final class SecondViewController: UIViewController, BlowinSwipeable {

override func viewDidLoad() {
super.viewDidLoad()

navigationItem.titleView = UILabel.navigationItemTitle("Second")

view.backgroundColor = UIColor(hex: ColorHex.purple)

let leftBackBarButtonItem = UIBarButtonItem(title: "",
Expand All @@ -39,8 +37,8 @@ final class SecondViewController: UIViewController, BlowinSwipeable {
navigationItem.setLeftBarButton(leftBackBarButtonItem, animated: true)
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
configureSwipeBack()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ final class NavigationController: UINavigationController {

override func viewDidLoad() {
super.viewDidLoad()

navigationBar.barTintColor = .darkGray
navigationBar.tintColor = .white

Expand Down

0 comments on commit f8621ac

Please sign in to comment.