15

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.

Mazyod
  • 22,319
  • 10
  • 92
  • 157
zambono
  • 1,397
  • 1
  • 17
  • 26

2 Answers2

43

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]; 
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
AechoLiu
  • 17,522
  • 9
  • 100
  • 118
  • 1
    thanks, after a lot more research i implemented this which is the same as your code CGPoint sliderPoint = [[slider superview] convertPoint:[slider center] toView:scrollView]; [4thView setCenter:CGPointMake([4th center].x,sliderPoint.y-20)]; – zambono Jan 12 '11 at 00:44
  • does - (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view take in concideration if there any transformation like zooming in the second view , if no how to do something like that – AMH Jun 30 '11 at 10:33
  • No, I don't think those methods take in consideration about any transformation. There are other methods to compute about point with transformation. – AechoLiu Aug 09 '11 at 00:40
  • What other methods would those be? I'm trying to calculate the point of a UIButton in a UITableView (UIScrollView) inside a view controller's view. convertPoint methods seem to give the position within the scroll view but I'm trying to get the visible coordinate position. EDIT: never mind, figured out I was converting to the scroll view's coordinates instead of the scroll view's parent (which was the visible area). – Kudit Oct 11 '12 at 01:06
  • For some reason, I had a problem using the point directly and needed to wrap it in CGPointMake(aPoint.x, aPoint.y). – LordParsley Jan 05 '18 at 10:42
5

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.

Nigel Greens Apps
  • 1,266
  • 1
  • 11
  • 6
  • this actually really helped me figure out what i needed, which was scrolling to a clicked button inside a scrollview – Tyler Dec 17 '16 at 05:22