当前位置: 动力学知识库 > 问答 > 编程问答 >

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

网友答案:

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]; 
网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: