ios - TouchBegan 在 iOS 12 中被调用,但在 iOS 13 中未被调用

标签 ios uigesturerecognizer touchesbegan ios13

我的 AppDelegate 中有一个 touchesBegan 方法。我用它来检测状态栏中的点击。我已经在那里用了很长时间并且效果很好。直到 iOS 13 测试版发布...

自从使用 iOS 13 以来,touchesBegan 已停止被调用。我的应用程序中与手势相关的其他任何内容都没有发生变化,我的 AppDelegate 中也没有任何变化。除了在 iOS 12 中调用 touchesBegan 而不是在 iOS 13 中调用之外,我真的没什么可说的。

到目前为止还没有运气,所以我在这里分享这个以防其他人遇到相同或类似的问题。

<小时/>

更新:1 我发现了以下半相关的问题,并引导我找到了Apple的新UISceneDelegate在 iOS 13 中。这些问题都不能解释为什么 touchesBegan 没有被调用。到目前为止,据我了解 UISceneDelegate ,我还没有找到为什么 touchesBegan 没有被调用。

View controller responds to app delegate notifications in iOS 12 but not in iOS 13
How to detect touches in status bar
iOS13: how to detect a status bar click event?

最佳答案

这就是我正在做的事情,可能不是很好,但到目前为止只是可行的解决方案。乐于采取改进。

  • 创建内容大于其框架的 TableViewController。
  • 将出现“On view”,滚动到表格 View 的末尾。
  • 将 TableViewController 的框架设置为与 StatusBarFrame 相同,并将其添加到 Window 的 Root View Controller
  • 在scrollViewShouldScrollToTop内部处理ScrollToTop事件并滚动到末尾以获取下一个事件。
  • 当 StatusBar 框架发生变化时,更新 TableViewController 的框架。

这也适用于 SceneDelegate。

ScrollToTopViewController.swift

class ScrollToTopViewController: UITableViewController {

    private let numberOfRow = 2
    private let cellIdentifier = "empty"

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.allowsSelection = false
        tableView.separatorColor = .clear
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
        view.autoresizingMask = []
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        scrollToBottom()
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return view.frame.size.height
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return numberOfRow
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) ?? UITableViewCell()
        cell.backgroundColor = .clear
        return cell
    }

    override func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            self.scrollToBottom()
        }
        print("HANDLE SCROLL TO TOP")
        return true
    }

    private func scrollToBottom() {
        tableView.scrollToRow(at: IndexPath(row: numberOfRow - 1, section: 0), at: .bottom, animated: false)
    }
}

AppDelegate.swift 或等效的 SceneDelegate 函数。

private let scrollToTopVC = ScrollToTopViewController()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    NotificationCenter.default.addObserver(self,
                                           selector: #selector(updateScrollToTopVC),
                                           name: UIApplication.didChangeStatusBarFrameNotification,
                                           object: nil)
    return true
}

func applicationDidBecomeActive(_ application: UIApplication) {
    updateScrollToTopVC()
}

@objc
private func updateScrollToTopVC() {
    guard let viewController = window?.rootViewController else {
        return
    }
    let statusBarFrame: CGRect

    if #available(iOS 13.0, *) {
        statusBarFrame = UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? .zero
    } else {
        statusBarFrame = UIApplication.shared.statusBarFrame
    }
    scrollToTopVC.view.frame = statusBarFrame
    viewController.addChild(scrollToTopVC)
    viewController.view.addSubview(scrollToTopVC.view)
    scrollToTopVC.didMove(toParent: viewController)
}

关于ios - TouchBegan 在 iOS 12 中被调用,但在 iOS 13 中未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57806710/

相关文章:

ios - Swift:像 Agar.io 一样平滑的 SKCameraNode 运动?

ios - 覆盖 iOS 上的 "Look Up"菜单项

ios - 使用移动触摸旋转 Sprite 节点

ios - 如何在 UIPageViewController 上禁用多点触控?

ios - 禁用手势下拉表单/页面表模式呈现

ios - 如何消除多次触摸的冲动(SpriteKit)

ios - 释放单例

ios - 未调用自定义 NSObject iniWithCoder

swift - 检测方向滑动 UISwipeGestureRecognizer

ios - 调用 touchesBegan 时动画 UIView 对象跳转