14
votes

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.

2

2 Answers

39
votes

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]; 
3
votes

Using the previous answer I took it a bit further to solve the issue I was having.

If you have multiple UITextView's within multiple UIView's all inside a single UIScrollView: this will animate and scroll to them.

This could be also be applied to UITextField's as well.

-(void)textViewDidBeginEditing:(UITextView *)textView {
    [self.theScrollView setContentOffset:[self.theScrollView convertPoint:CGPointMake(textView.frame.origin.x, textView.frame.origin.y) fromView:textView.superview] animated:YES];
}

If you have labels above your textviews, just offset the x or y by that amount.