xcode 在 UIScrollView 绝对屏幕位置内获取 UIButton

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5626411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:47:32  来源:igfitidea点击:

get UIButton inside an UIScrollView absolute screen location

xcodescreen-positioning

提问by CristiC

It may be a dumb question, but I can't find an answer to this ...

这可能是一个愚蠢的问题,但我找不到答案......

I have an UIScrollView and inside a number of UIButtons. When I press a button, I want to know the screen location of the button. If I use button.frame it gives me the position inside UIScrollView, which is normal.

我有一个 UIScrollView 和一些 UIButtons。当我按下按钮时,我想知道按钮的屏幕位置。如果我使用 button.frame 它会给我 UIScrollView 中的位置,这是正常的。

How can I find x and y relative to screen of the button pressed?

如何找到相对于按下按钮的屏幕的 x 和 y?

Thanks in advance.

提前致谢。

回答by Rob Lourens

Your objection was right- instead, use

你的反对是正确的 - 相反,使用

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

from UIView, like

来自 UIView,比如

[button.superview convertPoint:button.frame.origin toView:nil];

Not tested, but I believe that should work. Also see iPhone - Get Position of UIView within entire UIWindow

未经测试,但我认为应该可行。另请参阅iPhone - 在整个 UIWindow 中获取 UIView 的位置

回答by lenooh

For swift:

对于快速:

let pointXY:CGPoint = (button.superview?.convert(button.frame.origin, to: nil))!
print("button pointXY: \(pointXY.debugDescription)")