【问题标题】:get UIButton inside an UIScrollView absolute screen location在 UIScrollView 绝对屏幕位置内获取 UIButton
【发布时间】:2011-08-03 08:11:44
【问题描述】:

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

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

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

提前致谢。

【问题讨论】:

    标签: xcode screen-positioning


    【解决方案1】:

    你的反对是对的——相反,使用

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

    来自 UIView,喜欢

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

    未经测试,但我相信应该可以。另见iPhone - Get Position of UIView within entire UIWindow

    【讨论】:

    • 如果我水平滚动 UIScrollView,我可能有 button.frame.origin.x = 1017 什么的,所以你的例子不正确。
    【解决方案2】:

    对于快速:

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

    【讨论】:

    • 节省了我很多时间,我最近 3 个小时才找到这个,非常感谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    相关资源
    最近更新 更多