Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the position of an object within a UIView, within a UIScrollView

so lets say I have a UIScrollView, within it are 3 UIViews, within those there is a UISlider in each one. they are positioned vertically in the UIScrollView.

I now have a 4th UIView also in the UIScrollView which I wish to move around depending on the position of the slider which has been used.

so within my sliderChanged method which i pass the sender, i get the position of the slider, and adjust the position of the 4th UIWindow to its y. This works great on the first UIView, but once on another UIView which has forced me to scroll down, using the slider moves the 4th UIView but stays at the beginning of the UIScrollView

I am using:

[4thView setCenter:CGPointMake([4thView center].x, [slider center].y+10)];

what I need is to get the position of the slider relative to the content of the scrollView and not relative to its UIView, so that I may set the 4th view again relative to the scrollView content.

like image 868
zambono Avatar asked Jan 11 '11 23:01

zambono


1 Answers

You can convert the points by UIView's instance methods.

- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view

For example, I want to convert a ponint on the viewA to the scrollViewB's coordinates.

CGPoint aPtInScrollView = [viewA convertPoint:aPoint toView:scrollViewB]; 

Or, I want to know the position of viewA in scrollViewB.

 CGPoint aPosViewA = [scrollViewB convertPoint:CGPointZero fromView:viewA]; 
like image 86
AechoLiu Avatar answered Oct 21 '22 15:10

AechoLiu