首页 > 解决方案 > 触摸移动时找到 UIView 的交集

问题描述

在此处输入图像描述

如上图所示。我有一个 UIView 会响应我们的触摸事件。当我移动时,我需要找出它下面是哪个 UIView。对于移动 UIView 时的示例,如果它找到了图片上方的 UIView。我们需要找出它当前正在穿过这个 UIView

标签: iosswift

解决方案


将每个视图的bounds rectangles转换为一个共同的坐标系,然后查看矩形是否相交。您可以使用任一视图的坐标系作为公共系统,在这种情况下,您只需转换一个边界矩形。

extension UIView {
    func intersects(_ her: UIView) -> Bool {
        let herInMyGeometry = convert(her.bounds, from: her)
        return bounds.intersects(herInMyGeometry)
    }
}

推荐阅读