获取 UIScrollView 内容的可见矩形

Getting the visible rect of an UIScrollView#39;s content(获取 UIScrollView 内容的可见矩形)
本文介绍了获取 UIScrollView 内容的可见矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何找出屏幕上实际可见的显示视图内容的矩形 (CGRect).

How can I go about finding out the rect (CGRect) of the content of a displayed view that is actually visible on screen.

myScrollView.bounds

上面的代码在没有缩放时有效,但是一旦你允许缩放,它会在缩放比例不是 1 时中断.

The code above works when there's no zooming, but as soon as you allow zooming, it breaks at zoom scales other than 1.

为了澄清,我想要一个包含滚动视图内容的可见区域的 CGRect,相对于内容.(即,如果它是缩放比例 2,矩形的大小将是滚动视图大小的一半,如果它是缩放比例 0.5,它将是两倍.)

To clarify, I want a CGRect that contains the visible area of the scroll view's content, relative to the content. (ie. if it's a zoom scale 2, the rect's size will be half of the scroll view's size, if it's at zoom scale 0.5, it'll be double.)

推荐答案

回答我自己的问题,主要感谢 Jim Dovey 的回答,这并没有完全解决问题,但给了我回答的基础:

Answering my own question, mostly thanks to Jim Dovey's answer, which didn't quite do the trick, but gave me the base for my answer:

CGRect visibleRect;
visibleRect.origin = scrollView.contentOffset;
visibleRect.size = scrollView.bounds.size;

float theScale = 1.0 / scale;
visibleRect.origin.x *= theScale;
visibleRect.origin.y *= theScale;
visibleRect.size.width *= theScale;
visibleRect.size.height *= theScale;

主要区别在于 visibleRect 的大小应该是 scrollView.bounds.size,而不是 scrollView.contentSize 是内容视图的大小.还稍微简化了数学,并没有完全看到 isless() 的用途,它会在代码更大时破坏代码.

The main difference is that the size of the visibleRect ought to be scrollView.bounds.size, rather than scrollView.contentSize which is the size of the content view. Also simplified the math a bit, and didn't quite see the use for the isless() which would break the code whenever it's greater.

这篇关于获取 UIScrollView 内容的可见矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

UIButtons at the bottom of UIScrollView not working(UIScrollView 底部的 UIButtons 不起作用)
scrollViewWillEndDragging:withVelocity:targetContentOffset: not working on the edges of a UISCrollView(scrollViewWillEndDragging:withVelocity:targetContentOffset: 不在 UISCrollView 的边缘工作)
ImageView Scaling when scrolling down(向下滚动时 ImageView 缩放)
Bounds automatically changes on UIScrollView with content insets(UIScrollView 上的边界自动更改,带有内容插图)
iOS5 UITapRecognizer for UIScrollView interfering with buttons. How to fix?(用于 UIScrollView 的 iOS5 UITapRecognizer 干扰按钮.怎么修?)
How to Cancel Scrolling in UIScrollView(如何在 UIScrollView 中取消滚动)