获取 UIView 的可见矩形

发布于 2024-10-06 20:08:39 字数 174 浏览 0 评论 0原文

我有一个包含自定义 UIView 的 UIScrollView。在自定义 UIView 内部,我想知道它可见的矩形(即未剪切)。

快速的解决方案是让自定义 UIView 假设父级是 UIScrollView 并通过它获取内容大小,但我正在寻找一个不涉及 make 的更好的解决方案这样的假设。

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).

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不寐倦长更 2024-10-13 20:08:39

这应该可以解决问题,

CGRect visibleRect = CGRectIntersection(self.frame, superview.bounds);

在 UIView 中使用它,它应该为您提供代表该视图在其超级视图(UIScrollView)中的可见部分的矩形(如果有)。我在这里假设层次结构中它们之间没有视图,但如果有的话,修改代码应该是微不足道的。

希望我能帮忙!

This should do the trick

CGRect visibleRect = CGRectIntersection(self.frame, superview.bounds);

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.

Hope I could help!

夕色琉璃 2024-10-13 20:08:39

如果您能提供更多关于您想要实现的目标的信息,将会有所帮助。

如果你想知道超级视图的大小,你可以这样做:

CGRect superFrame = [self superview].frame;

It would help if you would give more info on what is that you are trying to accomplish.

If you want to know the size of the super view you can do this:

CGRect superFrame = [self superview].frame;
三人与歌 2024-10-13 20:08:39

雨燕3

extension UIView {
    var visibleRect: CGRect? {
        guard let superview = superview else { return nil }
        return frame.intersection(superview.bounds)
    }
}

Swift 3

extension UIView {
    var visibleRect: CGRect? {
        guard let superview = superview else { return nil }
        return frame.intersection(superview.bounds)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文