【问题标题】:How do I get the position of subview in my UITableViewCell with respect to the whole window?如何获取 UITableViewCell 中子视图相对于整个窗口的位置?
【发布时间】:2015-02-20 04:34:49
【问题描述】:

我知道convertRect:toRect 是这里的关键,但我不知道如何调用它。我有我的单元格的子视图,我希望它在应用程序的整个窗口中的位置。

当我在其目标操作方法中点击此按钮(子视图)时,我需要知道这一点。我需要在单元格上调用convertRect:toRect 函数,我想,为了获得正确的坐标,但在这种方法中我没有引用单元格本身。

我会爬上 superview 层次结构吗?这看起来很恶心,因为我不完全确定我会得到什么,因为它嵌入在单元格的 contentView 中,而且 Apple 用他们的私有子视图和诸如此类的东西做了一些古怪的事情。

代码:

@IBAction buttonPressed(sender: UIButton) {
    // This button got the callback that it was pressed. It's in a cell. I need to convert its rect to that of the window here. I need the coordinate system it's calculated from (the cell) which I'm not sure of how to easily do in a target action callback.
}

【问题讨论】:

  • @LyndseyScott 不,这是单元子视图上下文中的特定情况,在这种情况下获取视图的参考视图并不简单。
  • 你为什么这么认为?
  • @LyndseyScott 因为这是我正在努力解决的问题?如果在上述情况下获取子视图所在单元格的引用很简单,请分享您的解决方案。
  • 我同意 rmaddy 的解决方案。您不需要对单元格的引用......只需按钮。您无需将按钮转换为单元格的坐标系,然后将其转换为窗口的坐标系。一步完成。

标签: ios objective-c cocoa-touch uitableview uiview


【解决方案1】:

其实很简单:

CGRect windowRect = [someSubview convertRect:someSubview.bounds toView:nil];

假设您有一个按钮处理程序:

- (void)someButtonHandler:(id)sender {
    UIButton *button = sender;

    // do your stuff

    CGRect windowRect = [button convertRect:button.bounds toView:nil];
}

当然,如果您的方法设置为:

- (void)someButtonHandler:(UIButton *)button {
}

【讨论】:

  • 在问题中概述的这种情况下,我如何获得someSubview
  • 这取决于你。您的问题听起来好像您已经拥有子视图。如果不是,请澄清您的问题,因为不清楚您有什么。
  • 我确实有子视图,我认为这个问题已经说明了这一点。子视图位于单元格中。在子视图上调用convertRect 代码不会显示正确的结果,因为它的坐标系是根据它所在的单元格来表示的。
  • 用您想要转换子视图框架的实际代码更新您的问题。
  • 完成。我认为这与我概述的情况没有太大不同,但我希望它有所帮助。
【解决方案2】:

快速解决方案
myView的帧转换为parentView中对应的帧

let myViewGlobalFrame = myView.convert(myView.frame, to: parentView)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 2020-06-24
    相关资源
    最近更新 更多