通过UIViewController传递触摸 [英] Pass touches through a UIViewController

查看:109
本文介绍了通过UIViewController传递触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图控制器,我将其叠加在另一个视图控制器之上.我只需要顶视图控制器,这样我就可以覆盖一个临时弹出通知,并且具有覆盖的VC可以使我通过UITableViewController呈现此视图,因为您不能直接将子视图添加到tableview控制器中.

I have a view controller that I overlay on top of another view controller. I just need the top view controller so I can overlay a temporary pop up notification, and having an overlayed VC allows me to present this over a UITableViewController as you can't add subviews to tableview controllers directly.

当底部视图控制器具有另一个覆盖它的视图控制器时,是否可以与该底部视图控制器进行交互.如果这是视图或窗口,则可以通过将用户交互设置为false或使用hitTest来实现,但这两种方法都不适用于视图控制器.

Is it possible to interact with the bottom view controller while it has another view controller covering it. If this were a view or a window you would achieve this by setting user interaction to false or using hitTest but neither of these approaches works for a view controller.

推荐答案

创建这样的子类

class TouchDelegatingView: UIView {
    weak var touchDelegate: UIView? = nil


    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        guard let view = super.hitTest(point, with: event) else {
            return nil
        }

        guard view === self, let point = touchDelegate?.convert(point, from: self) else {
            return view
        }

        return touchDelegate?.hitTest(point, with: event)
    }
}

然后在ViewController设置视图类中(您可以直接在xib中或在loadView()方法中进行操作)

Then in your ViewController set view class (you can do it directly in xib, or inside loadView() method)

并添加

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        if let delegatingView = view as? TouchDelegatingView {
            delegatingView.touchDelegate = presentingViewController?.view
        }
    }
}

此外,您可以使用此机制将触摸委托给其他任何视图,无论它是否存在

Also you can use this mechanism to delegate touches to any other view, no matter if it

这篇关于通过UIViewController传递触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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