开发者

Current location of the object inside UIScrollView

开发者 https://www.devze.com 2022-12-12 18:14 出处:网络
I created UIScrollView and added large image inside of the scrollview to scroll. My question is how should I know the current location of the image that 开发者_StackOverflow社区I added inside of the s

I created UIScrollView and added large image inside of the scrollview to scroll. My question is how should I know the current location of the image that 开发者_StackOverflow社区I added inside of the scrollview like its X and Y? Like for example, I scroll the image horizontally then and I want to know what its new X and Y value of the image.


This is going to take a little bit of math. There are two properties (both CGPoints) that are of importance to you:

  • The frame.origin of your image, which is its position within its superview
  • The contentOffset of the scroll view, which is the visible portion of the scroll view

Basically what you want to do is grab the frame.origin.x and frame.origin.y properties of the image view, which will tell you where the image is relative to the absolute origin of the scroll view. Then, get the contentOffset.x and contentOffset.y of the scroll view and subtract them from the x and y of the image view. That should provide you the relative origin of the image view to your screen.


See scrollview.contentOffset. That should have the coordinates of the point that is visible in the top-left corner of the scrollview after any scrolling.

0

精彩评论

暂无评论...
验证码 换一张
取 消