問題描述
如何找出屏幕上實(shí)際可見的顯示視圖內(nèi)容的矩形 (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
上面的代碼在沒有縮放時(shí)有效,但是一旦你允許縮放,它會在縮放比例不是 1 時(shí)中斷.
The code above works when there's no zooming, but as soon as you allow zooming, it breaks at zoom scales other than 1.
為了澄清,我想要一個(gè)包含滾動視圖內(nèi)容的可見區(qū)域的 CGRect,相對于內(nèi)容.(即,如果它是縮放比例 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 的回答,這并沒有完全解決問題,但給了我回答的基礎(chǔ):
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;
主要區(qū)別在于 visibleRect 的大小應(yīng)該是 scrollView.bounds.size
,而不是 scrollView.contentSize
是內(nèi)容視圖的大小.還稍微簡化了數(shù)學(xué),并沒有完全看到 isless()
的用途,它會在代碼更大時(shí)破壞代碼.
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.
這篇關(guān)于獲取 UIScrollView 內(nèi)容的可見矩形的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!