Как вернуть жест смахивания в SwiftUI так же, как и в UIKit (interactivePopGestureRecognizer) - PullRequest
8 голосов
/ 04 октября 2019

Интерактивный распознаватель поп-жестов должен позволить пользователю вернуться к предыдущему виду в стеке навигации, когда он проведет дальше половины экрана (или что-то вокруг этих строк). В SwiftUI жест не отменяется, когда проведено недостаточно далеко.

SwiftUI: https://imgur.com/xxVnhY7

UIKit: https://imgur.com/f6WBUne


Вопрос:

Возможно ли получить поведение UIKit при использовании представлений SwiftUI?


Попытки

Я пытался встроить UIHostingController в UINavigationController, но это дает то же поведение, что и NavigationView.

struct ContentView: View {
    var body: some View {
        UIKitNavigationView {
            VStack {
                NavigationLink(destination: Text("Detail")) {
                    Text("SwiftUI")
                }
            }.navigationBarTitle("SwiftUI", displayMode: .inline)
        }.edgesIgnoringSafeArea(.top)
    }
}

struct UIKitNavigationView<Content: View>: UIViewControllerRepresentable {

    var content: () -> Content

    init(@ViewBuilder content: @escaping () -> Content) {
        self.content = content
    }

    func makeUIViewController(context: Context) -> UINavigationController {
        let host = UIHostingController(rootView: content())
        let nvc = UINavigationController(rootViewController: host)
        return nvc
    }

    func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {}
}

Ответы [ 2 ]

1 голос
/ 11 октября 2019

В итоге я переопределил значения по умолчанию NavigationView и NavigationLink, чтобы получить желаемое поведение. Это кажется настолько простым, что я должен пропустить что-то, что делают представления SwiftUI по умолчанию?

NavigationView

Я обертываю UINavigationController в супер простой UIViewControllerRepresentable, который дает UINavigationControllerпредставление содержимого SwiftUI как объект среды. Это означает, что NavigationLink может позже получить это, если он находится в том же контроллере навигации (представленные контроллеры представления не получают environmentObjects), что именно то, что нам нужно.

Примечание: NavigationView требуется .edgesIgnoringSafeArea(.top), и я пока не знаю, как установить это в самой структуре. Посмотрите пример, если ваш nvc обрезается сверху.

struct NavigationView<Content: View>: UIViewControllerRepresentable {

    var content: () -> Content

    init(@ViewBuilder content: @escaping () -> Content) {
        self.content = content
    }

    func makeUIViewController(context: Context) -> UINavigationController {
        let nvc = UINavigationController()
        let host = UIHostingController(rootView: content().environmentObject(nvc))
        nvc.viewControllers = [host]
        return nvc
    }

    func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {}
}

extension UINavigationController: ObservableObject {}

NavigationLink

Я создаю пользовательский NavigationLink, который обращается к средам UINavigationController, чтобы выдвинуть UIHostingController, содержащий следующий вид.

Примечание: Я не реализовал selection и isActive, которые есть у SwiftUI.NavigationLink, потому что я не до конца понимаю, что они делают. Если вы хотите помочь с этим, пожалуйста, прокомментируйте / отредактируйте.

struct NavigationLink<Destination: View, Label:View>: View {
    var destination: Destination
    var label: () -> Label

    public init(destination: Destination, @ViewBuilder label: @escaping () -> Label) {
        self.destination = destination
        self.label = label
    }

    /// If this crashes, make sure you wrapped the NavigationLink in a NavigationView
    @EnvironmentObject var nvc: UINavigationController

    var body: some View {
        Button(action: {
            let rootView = self.destination.environmentObject(self.nvc)
            let hosted = UIHostingController(rootView: rootView)
            self.nvc.pushViewController(hosted, animated: true)
        }, label: label)
    }
}

Это решает, что обратный удар не работает должным образом на SwiftUI, и потому что я использую имена NavigationView и NavigationLink, весь мой проект немедленно переключился на них.

Пример

В примере я также показываю модальное представление.

struct ContentView: View {
    @State var isPresented = false

    var body: some View {
        NavigationView {
            VStack(alignment: .center, spacing: 30) {
                NavigationLink(destination: Text("Detail"), label: {
                    Text("Show detail")
                })
                Button(action: {
                    self.isPresented.toggle()
                }, label: {
                    Text("Show modal")
                })
            }
            .navigationBarTitle("SwiftUI")
        }
        .edgesIgnoringSafeArea(.top)
        .sheet(isPresented: $isPresented) {
            Modal()
        }
    }
}
struct Modal: View {
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        NavigationView {
            VStack(alignment: .center, spacing: 30) {
                NavigationLink(destination: Text("Detail"), label: {
                    Text("Show detail")
                })
                Button(action: {
                    self.presentationMode.wrappedValue.dismiss()
                }, label: {
                    Text("Dismiss modal")
                })
            }
            .navigationBarTitle("Modal")
        }
    }
}

Редактировать: я начал с "Это кажется настолько простым, что я должен что-то упустить из виду", и я думаю, что нашел это. Похоже, что это не переводит EnvironmentObjects в следующее представление. Я не знаю, как по умолчанию NavigationLink делает это, поэтому сейчас я вручную отправляю объекты на следующий вид, где они мне нужны.

NavigationLink(destination: Text("Detail").environmentObject(objectToSendOnToTheNextView)) {
    Text("Show detail")
}

Редактировать 2:

Это открывает навигациюКонтроллер для всех видов внутри NavigationView, выполнив @EnvironmentObject var nvc: UINavigationController. Способ исправить это - сделать environmentObject, который мы используем для управления навигацией, классом fileprivate. Я исправил это в сущности: https://gist.github.com/Amzd/67bfd4b8e41ec3f179486e13e9892eeb

0 голосов
/ 09 октября 2019

Вы можете сделать это, спустившись в UIKit и используя свой собственный UINavigationController.

Сначала создайте файл SwipeNavigationController:

import UIKit
import SwiftUI

final class SwipeNavigationController: UINavigationController {

    // MARK: - Lifecycle

    override init(rootViewController: UIViewController) {
        super.init(rootViewController: rootViewController)
    }

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

        delegate = self
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        delegate = self
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // This needs to be in here, not in init
        interactivePopGestureRecognizer?.delegate = self
    }

    deinit {
        delegate = nil
        interactivePopGestureRecognizer?.delegate = nil
    }

    // MARK: - Overrides

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        duringPushAnimation = true

        super.pushViewController(viewController, animated: animated)
    }

    var duringPushAnimation = false

    // MARK: - Custom Functions

    func pushSwipeBackView<Content>(_ content: Content) where Content: View {
        let hostingController = SwipeBackHostingController(rootView: content)
        self.delegate = hostingController
        self.pushViewController(hostingController, animated: true)
    }

}

// MARK: - UINavigationControllerDelegate

extension SwipeNavigationController: UINavigationControllerDelegate {

    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        guard let swipeNavigationController = navigationController as? SwipeNavigationController else { return }

        swipeNavigationController.duringPushAnimation = false
    }

}

// MARK: - UIGestureRecognizerDelegate

extension SwipeNavigationController: UIGestureRecognizerDelegate {

    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        guard gestureRecognizer == interactivePopGestureRecognizer else {
            return true // default value
        }

        // Disable pop gesture in two situations:
        // 1) when the pop animation is in progress
        // 2) when user swipes quickly a couple of times and animations don't have time to be performed
        let result = viewControllers.count > 1 && duringPushAnimation == false
        return result
    }
}

Это то же самое SwipeNavigationController при условии здесь с добавлением функции pushSwipeBackView().

Эта функция требует SwipeBackHostingController, который мы определяем как

import SwiftUI

class SwipeBackHostingController<Content: View>: UIHostingController<Content>, UINavigationControllerDelegate {
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        guard let swipeNavigationController = navigationController as? SwipeNavigationController else { return }
        swipeNavigationController.duringPushAnimation = false
    }

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

        guard let swipeNavigationController = navigationController as? SwipeNavigationController else { return }
        swipeNavigationController.delegate = nil
    }
}

Затем мы настраиваем SceneDelegate приложения для использования SwipeNavigationController:

    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        let hostingController = UIHostingController(rootView: ContentView())
        window.rootViewController = SwipeNavigationController(rootViewController: hostingController)
        self.window = window
        window.makeKeyAndVisible()
    }

Наконец, используйте его в своем ContentView:

struct ContentView: View {
    func navController() -> SwipeNavigationController {
        return UIApplication.shared.windows[0].rootViewController! as! SwipeNavigationController
    }

    var body: some View {
        VStack {
            Text("SwiftUI")
                .onTapGesture {
                    self.navController().pushSwipeBackView(Text("Detail"))
            }
        }.onAppear {
            self.navController().navigationBar.topItem?.title = "Swift UI"
        }.edgesIgnoringSafeArea(.top)
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...