ios - 触摸移动时找到 UIView 的交集
问题描述
如上图所示。我有一个 UIView 会响应我们的触摸事件。当我移动时,我需要找出它下面是哪个 UIView。对于移动 UIView 时的示例,如果它找到了图片上方的 UIView。我们需要找出它当前正在穿过这个 UIView
解决方案
将每个视图的bounds rectangles转换为一个共同的坐标系,然后查看矩形是否相交。您可以使用任一视图的坐标系作为公共系统,在这种情况下,您只需转换一个边界矩形。
extension UIView {
func intersects(_ her: UIView) -> Bool {
let herInMyGeometry = convert(her.bounds, from: her)
return bounds.intersects(herInMyGeometry)
}
}
推荐阅读
- swift - Swift中负数的算术运算符
- maven - Maven 在编译期间抱怨“找不到符号”
- php - XMLHttpRequest 上传进度不准确百分比
- unity3d - 在纸板中渲染 ARCore
- rust - 结构中的预期生命周期参数
- reactjs - 当我们想要不同的 primaryTypographyProps 时替代 innerTheme
- objective-c - NSURL 中的 AVMetadata 返回 NULL URL
- python - Python版本之间的变量范围差异
- hibernate - 为什么Hibernate Hi-Lo算法策略将序列乘以50?
- c++ - 将一个类的对象列表传递给另一个类 c++