【发布时间】:2011-08-03 08:11:44
【问题描述】:
这可能是一个愚蠢的问题,但我找不到答案......
我有一个 UIScrollView 和一些 UIButtons。 当我按下一个按钮时,我想知道该按钮的屏幕位置。如果我使用 button.frame,它会给我 UIScrollView 内的位置,这是正常的。
如何找到相对于按下按钮屏幕的 x 和 y?
提前致谢。
【问题讨论】:
这可能是一个愚蠢的问题,但我找不到答案......
我有一个 UIScrollView 和一些 UIButtons。 当我按下一个按钮时,我想知道该按钮的屏幕位置。如果我使用 button.frame,它会给我 UIScrollView 内的位置,这是正常的。
如何找到相对于按下按钮屏幕的 x 和 y?
提前致谢。
【问题讨论】:
你的反对是对的——相反,使用
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
来自 UIView,喜欢
[button.superview convertPoint:button.frame.origin toView:nil];
未经测试,但我相信应该可以。另见iPhone - Get Position of UIView within entire UIWindow
【讨论】:
对于快速:
let pointXY:CGPoint = (button.superview?.convert(button.frame.origin, to: nil))!
print("button pointXY: \(pointXY.debugDescription)")
【讨论】: