使用上图,我想知道/检测UIView 3是否已到达UIScrollView的顶部。
到目前为止,我所做的是在UIScrollView中获取UIView的偏移量,然后检查它是否小于或等于0(零)。
let offsetY = scrollView.convert(myView.frame, to: nil).origin.y
if offsetY <= 0 {
print("myView is at Top")
}
但是上面的代码肯定是错误的!任何有想法的人吗?
TIA!
只需将滚动视图的顶部和v3的顶部转换为相同的坐标系,现在您只需看一下哪个较高即可。
让sv
成为滚动视图,让v3
成为视图3。然后:
func scrollViewDidScroll(_ sv: UIScrollView) {
let svtop = sv.frame.origin.y
let v3top = sv.superview!.convert(v3.bounds.origin, from:v3).y
if v3top < svtop { print("now") }
}
我实际上看不到您的方法有什么问题。由于根据documentation,当view
为nil时,该方法将转换为基于窗口的坐标:
Parameters
rect
A rectangle specified in the local coordinate system (bounds) of the receiver.
view
The view that is the target of the conversion operation. If view
is nil, this method instead converts to window base coordinates.
Otherwise, both view and the receiver must belong to the same
UIWindow object.