tags:

views:

40

answers:

2

Hi,

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

Thanks

+3  A: 

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.

Tim
actually not working, frame.origin and contentOffset's x and y always return 0 value.
sasayins
Then something is wrong in your code. I've used this many times before to do similar calculations.
Brad Larson
yeah, i just used wrong variable format in my NSLog, working now. thanks.
sasayins
+1  A: 

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.

mahboudz
it seems the contentOffset's x and y always zero, not updating everytime I scroll the image inside of the UIScrollView.
sasayins
Show us some code. I just checked and my scrollView will change the contentOffset when scrolling happens.
mahboudz
yup, my problem, i used %d instead of %f in the NSLog. yay :)
sasayins