如何检查一个视图是否在另一个视图之上?
How to check to see if one view is on top of another view?
目前有一个位于 uiview(屏幕底部)后面的滚动视图(整个屏幕)。我正在尝试检查滚动条上的标签是否被 uiview 覆盖(这发生在 iphone se 上)。有没有办法检查 uiview 是否覆盖了滚动条上的标签?我试过了使用滚动视图上标签的中心 cgpoint 来查看它是否小于 uiview 上的一个点,但它不允许我比较两个 cgpoints 的大小:
if atmLabel.center < CGPoint(x: 156, y: 570) {
buttonView.dropShadow(shadowOpacity: 0.5)
}
对于点检查,你应该使用
包含 CGRect 的功能...简而言之,这应该可以工作
let view = UIView()
let label = UILabel()
if label.frame.contains(view.frame) {
//Do your stuff...
}
您可以通过 Xcode 和此按钮
使用视图层次结构
或者您可以简单地打印当前 viewController 个子视图
print(self.view.subviews)
这会打印出 UIView 子类数组,这些子类按视图层的升序排序...我推荐第一种调试方式:)
如果您想查看两个视图在代码中是否重叠,我认为这应该可行:
if view1.frame.intersects(view2.frame) {
// handle overlap
}
您的视图层次结构:
parentView
- scrollView
- - label
- topView
The frame rectangle, which describes the view’s location and size in its superview’s coordinate system.
https://developer.apple.com/documentation/uikit/uiview/1622621-frame
你不能简单地比较 label
和 topView
的帧,因为 frame
是父视图坐标中的位置,也就是 topView.frame
是 parentView
坐标中的位置label.frame
在 scrollView
坐标中。
因此您需要将 label
转换为 parentView
或将 topView
转换为 scrollView
坐标。让我们来看看第一个变体:
let frameInParent = parentView.convert(label.frame, from: label.superview)
之后你可以检查这个框架是否与topView
的相交:
let isOverlaps = frameInParent.intersects(topView.frame)
但这仅在您不对视图使用任何转换时才成立,因为框架文档同样如此:
Warning
If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.
目前有一个位于 uiview(屏幕底部)后面的滚动视图(整个屏幕)。我正在尝试检查滚动条上的标签是否被 uiview 覆盖(这发生在 iphone se 上)。有没有办法检查 uiview 是否覆盖了滚动条上的标签?我试过了使用滚动视图上标签的中心 cgpoint 来查看它是否小于 uiview 上的一个点,但它不允许我比较两个 cgpoints 的大小:
if atmLabel.center < CGPoint(x: 156, y: 570) {
buttonView.dropShadow(shadowOpacity: 0.5)
}
对于点检查,你应该使用
包含 CGRect 的功能...简而言之,这应该可以工作
let view = UIView()
let label = UILabel()
if label.frame.contains(view.frame) {
//Do your stuff...
}
您可以通过 Xcode 和此按钮
或者您可以简单地打印当前 viewController 个子视图
print(self.view.subviews)
这会打印出 UIView 子类数组,这些子类按视图层的升序排序...我推荐第一种调试方式:)
如果您想查看两个视图在代码中是否重叠,我认为这应该可行:
if view1.frame.intersects(view2.frame) {
// handle overlap
}
您的视图层次结构:
parentView
- scrollView
- - label
- topView
The frame rectangle, which describes the view’s location and size in its superview’s coordinate system.
https://developer.apple.com/documentation/uikit/uiview/1622621-frame
你不能简单地比较 label
和 topView
的帧,因为 frame
是父视图坐标中的位置,也就是 topView.frame
是 parentView
坐标中的位置label.frame
在 scrollView
坐标中。
因此您需要将 label
转换为 parentView
或将 topView
转换为 scrollView
坐标。让我们来看看第一个变体:
let frameInParent = parentView.convert(label.frame, from: label.superview)
之后你可以检查这个框架是否与topView
的相交:
let isOverlaps = frameInParent.intersects(topView.frame)
但这仅在您不对视图使用任何转换时才成立,因为框架文档同样如此:
Warning
If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.