touchesBegan在iOS 12中被调用,但在iOS 13中未被调用 [英] touchesBegan is called in iOS 12 but is not called in iOS 13

查看:217
本文介绍了touchesBegan在iOS 12中被调用,但在iOS 13中未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的AppDelegate中有一个touchesBegan方法.我用它来检测状态栏中的水龙头.我在那里待了很长时间,效果很好.在iOS 13 Beta发布之前...

I have a touchesBegan method in my AppDelegate. I use it to detect taps in the status bar. I've had it there for a long time and it's worked great. Until the iOS 13 betas came out...

自从使用iOS 13以来,touchesBegan已经停止调用.我的应用程序中与手势相关的所有其他内容均未更改,AppDelegate中的任何内容均未更改.除了在iOS 12中调用touchesBegan而不在iOS 13中调用touchesBegan之外,我实际上没有什么可做的.

Ever since using iOS 13 touchesBegan has stopped being called. Nothing else related to gestures has changed in my app, nothing in my AppDelegate has changed. I don't really have much to go on other than touchesBegan is called in iOS 12 and not in iOS 13.

尽管到目前为止还没有运气,所以如果有人遇到相同或相似的问题,我在这里分享.

No luck so far though so I'm sharing this here in case someone else has had the same or a similar issue.

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

Update: 1 I have found the following issues which are semi related and have lead me to Apple's new UISceneDelegate in iOS 13. None of these issues explain why touchesBegan isn't being called. And as I understand the UISceneDelegate so far, I haven't yet found why touchesBegan isn't being called.

查看控制器在iOS 12中响应应用程序委托通知,但在iOS 13中不响应
如何在状态栏中检测触摸
iOS13:如何检测状态栏点击事件?

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?

推荐答案

这就是我正在做的,可能不是很好,但到目前为止只能解决问题.很高兴进行改进.

This is what I am doing, may not be great but only working solution so far. Happy to take improvements.

  • 创建具有大于框架内容的TableViewController.
  • 将出现在视图上,滚动到表格视图的末尾.
  • 将TableViewController的框架设置为与StatusBarFrame相同,并将其添加到Window的根viewcontroller
  • 在scrollViewShouldScrollToTop内部处理ScrollToTop事件,然后滚动到末尾以获取下一个事件.
  • 在StatusBar框架更改时,更新TableViewController的框架.

此功能也可与SceneDelegate一起使用.

This works works with SceneDelegate as well.

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

这篇关于touchesBegan在iOS 12中被调用,但在iOS 13中未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆