【问题标题】:Pull-Down gesture not working with uipageViewController下拉手势不适用于 uipageViewController
【发布时间】:2022-12-30 00:00:34
【问题描述】:

我遇到无法正常工作的下拉手势问题。我正在展示一个 UIPageViewController,每个页面都是一个包含 UITableViewUIViewController。当过渡样式设置为pageCurl时,关闭下拉手势没有问题,但是当过渡样式设置为scroll时,我无法关闭视图。即使在顶部,也只有 UITableView 在滚动

final class HistoryReceiptContainerViewController: UIPageViewController {

    private let viewModel: HistoryReceiptContainerViewModel
    
    init(viewModel: HistoryReceiptContainerViewModel) {
        self.viewModel = viewModel
        super.init(transitionStyle: .pageCurl, navigationOrientation: .horizontal, options: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        setViewControllers([receiptViewController(for: HistoryReceiptViewModel(historyId: "1234", source: "", context: nil, contextId: nil))], direction: .forward, animated: true, completion: nil)
    }
    
    private func receiptViewController(for viewModel: HistoryReceiptViewModel) -> HistoryReceiptViewController {
        let vc = HistoryReceiptViewController(viewModel: viewModel)
        return vc
    }
}

以及呈现视图的代码

present(HistoryReceiptContainerViewController(viewModel: viewModel)

你有解决这类问题的方法吗?

先感谢您!

https://imgur.com/a/rwkVA0Q

【问题讨论】:

  • 我遇到了同样的问题。您是否同时找到了解决方案?

标签: ios swift


【解决方案1】:

下面是一个示例实现,通过添加 UIPanGestureRecognizer 和一些自定义手势识别器处理来解决使用 transitionStyle .scroll 关闭 UIPageViewController 的问题。当将 UIPageViewController 推送到导航堆栈以及以模态方式呈现时,它将起作用。

class DemoPageViewController: UIPageViewController, UIGestureRecognizerDelegate {
 
    var presentedModally = false

    init(presentedModally: Bool = false) {
        super.init(transitionStyle: .scroll, //if you would use .pageCurl dismissing works
                   navigationOrientation: .horizontal,
                   options: nil)
        self.presentedModally = presentedModally
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpPanGestureForDismissingSelf()
    }

    // MARK: - UIGestureRecognizerDelegate
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        guard let navigationController = navigationController, navigationController.interactivePopGestureRecognizer == gestureRecognizer else {
            return true
        }
        return navigationController.viewControllers.count > 1
    }

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return !presentedModally
    }

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

    private func setUpPanGestureForDismissingSelf() {
        guard presentedModally else { return }
        let panGestureRecognizer = UIPanGestureRecognizer(target: self,
                                                          action: #selector(handlePanGesture(sender:)))
        panGestureRecognizer.delegate = self
        view.addGestureRecognizer(panGestureRecognizer)
    }

    @objc func handlePanGesture(sender: UIPanGestureRecognizer) {
        let dragVelocity = sender.velocity(in: view)
        if dragVelocity.y >= 1300 {
            dismiss(animated: true)
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多