Question

I have scrollView in which there are 12 buttons. On window only four buttons are visible at a time. For animation purpose I need button's position regarding to the window.

I had followed these methods but its not working for me:

[btn.superview convertPoint:btn.frame.origin toView:nil];

btnImgeView.frame= CGRectMake(btn.frame.origin.x + 10, 240, btn.frame.size.width, btn.frame.size.height);

btn.frame.origin.x is taking coordinate from scrollView's origin not from the origin of the screen.

how can I get the button's position regarding to the window?

Was it helpful?

Solution

From your description it sounds like what you really want to do is display an image view where an button appears, but its location is inside a scroll view.

As suspected, calculating screen coordinates is not the correct approach here. Instead, you need to offset the button's rect/origin with the scroll view's displacement to determine the correct position.

Use the scroll view's -contentOffset property to get a CGPoint indicating the, well, offset of the content. Subtract that point from the button's bounds origin (or just center if you would like something more convenient). That will give you the correct point at which to display your image (relative to the scroll view's frame origin).

If you aren't displaying the image in the scroll view, you can transform the point to the superview's coordinate system with -[UIView convertPoint:fromView:] or [UIView convertPoint:toView:].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top