Pop view controller using Screen edge pan gesture recogniser not following the thumb
There's no need to add Edge Pan Gesture Recogniser. Following @beyowulf's suggestions I've been able to implement the swipe to go back feature that behaves the same way as the default system implementation does - the views edge follows my thumb as I swipe it to dismiss it.
So I've removed the ScreenEdge Pan Gesture Recogniser
from the storyboard and also removed the related @IBAction
.
I've made my first view controller to be the delegate for interactivePopGestureRecognizer
. Here's the code:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.interactivePopGestureRecognizer?.delegate = self
}
}
extension ViewController: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
return navigationController?.viewControllers.count > 1 ? true : false
}
}
How to handle a UIScreenEdgeGestureRecognizer with a map view covering the whole view?
What I did was to add a view with a 10pt width and clear color background over the edge I needed the gesture recognizer and I added the gesture recognizer to this view.
In the picture, the white rectangle on the right is the view which will have the gesture recognizer. You should change the background color to clear color.
The view outlet:
@property (weak, nonatomic) IBOutlet UIView *rightEdgeGestureView;
Add the gesture recognizer to the view on the screen right edge:
- (void)setupGestureRecognizer {
UIScreenEdgePanGestureRecognizer *gestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer.edges = UIRectEdgeRight;
[_rightEdgeGestureView addGestureRecognizer:gestureRecognizer];
}
Is there a way to call a function once a View Controller leaves the view stack?
Yes, I can think of few ways to do this off the top of my head
One option would be to add some code to a dealloc method of the UIViewController.
If you don't expect the view controller controller to get deallocated when it leaves the stack you can also set a UINavigationControllerDelegate for the UINavigationController and define
func navigationController(_ navigationController: UINavigationController,
didShow viewController: UIViewController,
animated: Bool) {
guard let poppedViewController =
navigationController.transitionCoordinator?.viewController(forKey: .from)
<Do something with the popped VC>
GestureRecogniser not working when going back
Call [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
in viewWillAppear
instead of viewDidLoad
so that it is replaced following each transition. A gesture can only be attached to one view at a time so when you attach it to a different view it gets removed from the first.
Alternatively, consider adding the gesture to a more root view (like the window / navigation bar) depending on what interaction you want to enable.
Related Topics
How to Return a Button from a Function in Swiftui
Not Getting Expected Delegate Calls When Trying to Restore In-App Purchases with Storekit
Swift: Reusable Uiview in Storyboard and Sizing Constraints
How to Record My MAC's Internal Sound, Not the Microphone!, Using Avcapturesession
Setting Arkit Orientation via Swift
How to Get a Double Value Up to 2 Decimal Places
Swift Find Superview of Given Class with Generics
Why Does Using Dynamictype on a Force Unwrapped Nil Optional Value Type Work
Swift Conditional Conformances with Generic Type
Convert to Latest Swift Syntax' Breaks the Build Even When There Are No Changes
How to Send Data from iPhone to Watchkit in Os2 in Swift
Scntext Alignment Not Working in iOS
Macos App Local Notification Not Showing When Testing with Xcode
Content Blocker Extension with a String Instead of a File
How to Compile "Hello World" Program with Swift on Ubuntu 14.04