Skip to content

Commit

Permalink
BackGesture helper (#141)
Browse files Browse the repository at this point in the history
* ✨ Add BackGesture helper

* πŸ’š Hide import behind compiler directive

* πŸ“ Update CHANGELOG.md
  • Loading branch information
leinhauplk authored Dec 1, 2023
1 parent 99b74ba commit 31e80fa
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ACKategories.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
69FA5FBC23C868A900B44BCD /* AppFlowCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69FA5FAB23C868A900B44BCD /* AppFlowCoordinator.swift */; };
69FA5FBD23C868A900B44BCD /* ModalFlowCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69FA5FAC23C868A900B44BCD /* ModalFlowCoordinator.swift */; };
69FA5FC223C8690A00B44BCD /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 69FA5FC123C8690A00B44BCD /* LaunchScreen.storyboard */; };
6A72B2222B1A15AC00A59EDD /* BackGesture.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A72B2212B1A15AC00A59EDD /* BackGesture.swift */; };
88EDD90425B8252E00207987 /* GradientViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88EDD90325B8252E00207987 /* GradientViewController.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -244,6 +245,7 @@
69FA5FAB23C868A900B44BCD /* AppFlowCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppFlowCoordinator.swift; sourceTree = "<group>"; };
69FA5FAC23C868A900B44BCD /* ModalFlowCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ModalFlowCoordinator.swift; sourceTree = "<group>"; };
69FA5FC123C8690A00B44BCD /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = LaunchScreen.storyboard; sourceTree = "<group>"; };
6A72B2212B1A15AC00A59EDD /* BackGesture.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackGesture.swift; sourceTree = "<group>"; };
88EDD90325B8252E00207987 /* GradientViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GradientViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -370,6 +372,7 @@
69E0A5F62AFD10BE00C8E8D9 /* PropertyWrappers */,
69E0A5FE2AFD10BE00C8E8D9 /* RandomExtensions */,
69E0A6032AFD10BE00C8E8D9 /* SwiftUIExtensions */,
6A72B2212B1A15AC00A59EDD /* BackGesture.swift */,
);
path = ACKategories;
sourceTree = "<group>";
Expand Down Expand Up @@ -847,6 +850,7 @@
69ACD6F72AFD133A0021127B /* UIStackViewExtensions.swift in Sources */,
69ACD6F82AFD133A0021127B /* UIView+Spacer.swift in Sources */,
69ACD6F92AFD133A0021127B /* UIViewController+Children.swift in Sources */,
6A72B2222B1A15AC00A59EDD /* BackGesture.swift in Sources */,
69ACD6FA2AFD133A0021127B /* UIViewController+FrontMost.swift in Sources */,
69ACD6FB2AFD133A0021127B /* UIViewExtensions.swift in Sources */,
69ACD6FC2AFD133A0021127B /* UserDefaultsExtensions.swift in Sources */,
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
```

## Next
- Support tvOS & watchOS, use single multiplatform target for Carthage ([#14%](https://github.com/AckeeCZ/ACKategories/pull/140), kudos to @olejnjak)
- Add helper function for easier back gesture setup ([#141](https://github.com/AckeeCZ/ACKategories/pull/141), kudos to @leinhauplk)
- Support tvOS & watchOS, use single multiplatform target for Carthage ([#140](https://github.com/AckeeCZ/ACKategories/pull/140), kudos to @olejnjak)
- Add helpers for `SwiftUI.EdgeInsets` ([#138](https://github.com/AckeeCZ/ACKategories/pull/138), kudos to @olejnjak)
- Bump deployment target to iOS 12 ([#137](https://github.com/AckeeCZ/ACKategories/pull/137), kudos to @olejnjak)
- Add Font modifier for SwiftUI fonts ([#134](https://github.com/AckeeCZ/ACKategories/pull/134), kudos to @leinhauplk)
Expand Down
56 changes: 56 additions & 0 deletions Sources/ACKategories/BackGesture.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#if os(iOS)
import UIKit

private class BackGestureDelegate: NSObject {
// MARK: - Private properties

private weak var navigationController: UINavigationController?

// MARK: Initializers

init(
navigationController: UINavigationController
) {
super.init()
self.navigationController = navigationController
}
}

// MARK: - UIGestureRecognizerDelegate
extension BackGestureDelegate: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
navigationController.map { $0.viewControllers.count > 1 } ?? false
}
}

extension UINavigationController {
private enum Keys {
static var backGestureDelegate: UInt8 = 0
}

private var backGestureDelegate: BackGestureDelegate {
if let delegate = objc_getAssociatedObject(self, &Keys.backGestureDelegate) as? BackGestureDelegate {
return delegate
}

let delegate = BackGestureDelegate(navigationController: self)
objc_setAssociatedObject(
self,
&Keys.backGestureDelegate,
delegate,
.OBJC_ASSOCIATION_RETAIN
)
return delegate
}

/// Add custom interactivePopGestureRecognizer delegate so it works even with hidden navigationBar
public func setupCustomBackGestureDelegate() {
assert(isViewLoaded, "View needs to be already loaded")
assert(
interactivePopGestureRecognizer != nil,
"Cannot install gesture delegate on nil recognizer"
)
interactivePopGestureRecognizer?.delegate = backGestureDelegate
}
}
#endif

0 comments on commit 31e80fa

Please sign in to comment.