简体   繁体   中英

Full Screen Pan Swipe Gesture on NavigationView?

Problem: How can I increase the range of the swipe gesture when popping out of a navigation view? Currently swipes are only registered on a small margin from the left of the device. I would like to make the swipe be registered on the entire screen like instagram you can swipe from anywhere. 在此处输入图像描述

I have this code which enables swiping again after hiding the nav bar with.navigationBarHidden(true). I just cannot figure out how to increase the swiping range. Any thoughts? Thanks

extension UINavigationController: UIGestureRecognizerDelegate {
    override open func viewDidLoad() {
        super.viewDidLoad()
        interactivePopGestureRecognizer?.delegate = self
        
    }

    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return viewControllers.count > 1
    }
}

Similar Solution: I have found this answer to the same question I can't figure out how to get it to work. There are no errors, I can't get any response unless I unhide the navbar Swipe to go back only works on edge of screen?

For a SwiftUI perspective, you can add swipe gesture as a modifier and add it to your SwiftUI View.

For a swipe gesture to be accessible on all the view, you can use .contentShape(Rectangle()) .

Here, is the modifier for a swipe right gesture on a View:

import SwiftUI

struct GestureSwipeRight: ViewModifier {

  func body(content: Content) -> some View {
    content
      .contentShape(Rectangle())  // This is what would make gesture
      .gesture(                   // accessible on all the View.
        DragGesture(minimumDistance: 30, coordinateSpace: .local)
          .onEnded { value in
            if value.translation.width > .zero
                && value.translation.height > -30
                && value.translation.height < 30 {

              // Add your actions here when user swipe right.
            }
          }
      )
  }
}

You can then add this gesture to any view when you need it this way:

import SwiftUI

struct ContentView: View {

  var body: some View {
    VStack {
      // Your view body declaration
    }
    .modifier(GestureSwipeRight())
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM