touchesBegan在iOS 12中被调用,但在iOS 13中未被调用。

11
我在我的AppDelegate中有一个touchesBegan方法,用于检测状态栏中的轻击。我已经使用它很长时间了,并且一直运行得很好。直到iOS 13 beta发布之后...

自从使用iOS 13以来,touchesBegan就不再被调用了。我的应用程序中与手势相关的其他内容都没有改变,我的AppDelegate也没有改变。我真的没有什么可依据,除了touchesBegan在iOS 12中被调用而在iOS 13中没有被调用。

目前还没有什么进展,所以我在这里分享一下,以防其他人遇到相同或类似的问题。


更新:1 我发现以下问题与此问题有些关联,并引导我查看了苹果新的UISceneDelegate。但是这些问题都无法解释为什么touchesBegan没有被调用。就我目前对UISceneDelegate的理解,我还没有找到touchesBegan为什么没有被调用的原因。

在iOS 12中,视图控制器响应AppDelegate通知,在iOS 13中不响应

如何检测状态栏的触摸事件
iOS 13:如何检测状态栏的点击事件?


你找到什么了吗?我也遇到了同样的问题,一直无法解决。 - Miguel
@Miguel,目前还没有,如果我有更新的话,我会在这里通知你。 - Xeaza
@Xeaza 谢谢你的研究。我也遇到了同样的问题,似乎在 iOS 13 上,应该在 SceneDelegate 上检测轻击,但这是否意味着应用程序必须放弃对 <iOS 13 的支持? - Nikolay Spassov
2个回答

1

这是我正在做的事情,可能不是很好但是目前只有这个可行的解决方案。欢迎提出改进意见。

  • 创建一个TableViewController,其内容大于其框架。
  • 在视图将要出现时,滚动到表格视图的末尾。
  • 将TableViewController的框架设置为StatusBarFrame并将其添加到Window的根视图控制器中。
  • 在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函数。

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)
}

0

我最近也遇到了同样的问题。花了我几个小时才解决了它。 我创建了另一个UIWindow来显示应用程序通知/警报,位于主窗口之上。由于某种原因,在使用iOS 13时,它会吞噬所有触摸操作。我的解决方法是只需禁用顶部窗口上的用户交互即可。但这意味着您显然无法与通知/警报进行任何用户交互。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接