【问题标题】:convertRect to get relative position in super viewconvertRect 获取超级视图中的相对位置
【发布时间】:2012-09-05 00:05:18
【问题描述】:

我有一堆放在 UIViewController1 中的水平 UITableView。我想找到被点击的 UITableViewCell 的位置,我想根据 UIViewController1 获得位置。所以这就是我在 didSelectRowAtIndexPath 中所做的:

MyCell *cell = (MyCell *)[tableView cellForRowAtIndexPath:indexPath];

                UIView *animatedView = [[UIView alloc] initWithFrame:cell.frame];
 [animatedView setFrame:[animatedView convertRect:animatedView.frame toView:self.newsRackController.view]];

但是,这似乎不能正确转换它。我做错了什么?

【问题讨论】:

标签: iphone objective-c ios ipad uiview


【解决方案1】:

您需要从 UITableView 的视图中转换 UITableViewCell 的 CGRect,然后再将其转换为您的视图控制器视图。

但除了这些,也许你可以使用 UITableView 的方法

- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath

获取此 CGRect 并将其转换为您的 UIViewController 的视图。

【讨论】:

  • 谢谢,很好的回答!
  • 对我来说,这个解决方案没有考虑 contentOffset 或 contentInset。
【解决方案2】:

这是因为animatedView 不属于任何父母,而您将frame 用作bounds。您应该首先找到框架并相应地初始化animatedView

MyCell *cell = (MyCell *)[tableView cellForRowAtIndexPath:indexPath];
CGRect animatedViewFrame = [tableView convertRect:cell.frame toView:self.newsRackController.view];
UIView *animatedView = [[UIView alloc] initWithFrame:animatedViewFrame];

【讨论】:

  • 谢谢!这实际上是对问题的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多