获取UIView的可见矩形 [英] Getting a UIView's visible rectangle
问题描述
我有一个 UIScrollView
,其中包含自定义 UIView
。在自定义 UIView
中,我想知道它可见的矩形(即没有剪切)。
I have a UIScrollView
that contains a custom UIView
. Inside the custom UIView
, I'd like to know the rectangle in which it's visible (i.e. not clipped).
快速肮脏的解决方案是让自定义UIView假设父级是 UIScrollView
并通过它获取内容大小,但我正在寻找更好的解决方案不涉及做出这样的假设。
The quick-n-dirty solution is to have the custom UIView assume that the parent is a UIScrollView
and get the content size through it, but I'm looking for a better solution that doesn't involve make such assumptions.
推荐答案
这应该可以解决这个问题
This should do the trick
CGRect visibleRect = CGRectIntersection(self.frame, superview.bounds);
在UIView中使用它,它应该会得到代表可见部分的矩形(如果有的话)在它的超级视图(UIScrollView)中的视图。我假设它们在层次结构中没有它们之间的视图,但是如果有的话,摆弄代码应该是微不足道的。
Use that in the UIView and it should get you the rectangle (if any) that represents the visible section of that view in it's superview (The UIScrollView). I'm assuming here that there is no view between them in the hierarchy, but if there is, fiddling the code should be trivial.
希望我能帮忙!
这篇关于获取UIView的可见矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!