简体   繁体   English

如果我向下滚动或不向下滚动,我将在UIScrollView中获得与UIButton相同的位置

[英]I get the same position of a UIButton inside a UIScrollView if I scroll down or not

I want to get the position of a UIButton inside a UIScrollView the problem is that i get the same position for when the UIButton is in it's original position or if I scroll down… 我想在UIScrollView中获取UIButton的位置,问题是当UIButton处于其原始位置或向下滚动时,我得到的位置相同。

I'm using NSLog to print the position and I get the same output 我正在使用NSLog打印位置,并且得到相同的输出

  • Without scroll down i get... 没有向下滚动,我得到...

     2014-11-05 19:28:02.564 ... {{260, 163}, {370, 225}} 
  • Even if I scroll down I get the same output... 即使向下滚动我也会得到相同的输出...

     2014-11-05 19:28:06.052 ... {{260, 163}, {370, 225}} 

This is the code i'm using to obtain the position of the UIButton inside the UIScrollView ... 这是我用来获取UIScrollView中 UIButton位置的代码...

NSLog(@"%@", NSStringFromCGRect(CGRectMake(myButton.frame.origin.x, myButton.frame.origin.y, myScrollDown.frame.size.width, myScrollDown.frame.size.height)));

There is a way to get the actual position of the UIButton even after I scrolled down? 即使我向下滚动后,有没有办法获取UIButton的实际位置?

There is another way to get the position of the UIButton inside the ScrollView? 还有另一种方法来获取UIButton在ScrollView中的位置吗?

I've already visited the following links and I have not resolved it yet. 我已经访问了以下链接,但尚未解决。

get UIButton inside an UIScrollView absolute screen location 在UIScrollView绝对屏幕位置内获取UIButton

iPhone - Get Position of UIView within entire UIWindow iPhone-在整个UIWindow中获取UIView的位置

Cocoa: What's the difference between the frame and the bounds? 可可:框架和边界有什么区别?

Thanks for any help! 谢谢你的帮助!

也许你可以试试看

NSLog(NSStringFromCGRect([self.view convertRect:myButton.frame fromView:myButton.superview]));

You're getting the position within the scrollview. 您将获得滚动视图中的位置。 Probably what you want is the position within the scrollview's superview (perhaps self.view, perhaps something else). 您可能想要的是scrollview的superview中的位置(也许是self.view,也许还有其他东西)。

Your scroll view is going to have a contentOffset value. 您的滚动视图将具有contentOffset值。 The frame of the subviews inside of the scroll view don't change when it scrolls. 滚动视图内部的子视图的框架在滚动时不会改变。 Instead, this contentOffset value is changed. 相反,此contentOffset值已更改。 I'm sure there are better ways to do this but on a basic level, you could just do something like so to get the frame in the scroll view's superview: 我敢肯定,有更好的方法可以做到这一点,但是从基本的角度来说,您可以做一些类似的事情来使框架成为滚动视图的超级视图:

CGRect frame = myButton.frame;
frame.origin.x -= scrollView.contentOffset.x;
frame.origin.y -= scrollView.contentOffset.y;
NSLog(@"%@", NSStringFromCGRect(frame);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM