如何获得子视图在UITableViewCell中相对于整个窗口的位置? [英] How do I get the position of subview in my UITableViewCell with respect to the whole window?
问题描述
我知道 convertRect:toRect
是这里的关键,但是我不确定如何调用它。我有一个单元格的子视图,我想要它在应用程序整个窗口中的位置。
I know convertRect:toRect
is the key here, but I'm not sure how to call it. I have a subview of my cell, and I want its position in the app's entire window.
在目标操作方法中点击此按钮(子视图)时,我需要知道这一点。我想,我想在单元格上调用 convertRect:toRect
函数,以获得正确的坐标,但是在这种方法中,我没有引用单元格本身。
I need to know this when I tap on this button (the subview) in its target action method. I need to call the convertRect:toRect
function on the cell, I figure, in order to get the right coordinates, but in this method I have no reference to the cell itself.
我是否爬上了超级视图层次结构?这似乎很糟糕,因为我不太确定会得到什么,因为它已嵌入单元格的contentView中,而Apple确实使用其私人子视图和其他功能进行了古怪的操作。
Do I climb the superview hierarchy? That seems gross, as I'm not totally sure what I would get, as it's embedded in the contentView of the cell, and Apple does wacky stuff with their private subviews and whatnot.
代码:
@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.
}
推荐答案
实际上非常简单:
CGRect windowRect = [someSubview convertRect:someSubview.bounds toView:nil];
假设您有一个按钮处理程序:
Assuming you have a button handler:
- (void)someButtonHandler:(id)sender {
UIButton *button = sender;
// do your stuff
CGRect windowRect = [button convertRect:button.bounds toView:nil];
}
当然,如果将方法设置为:
Of course it's even easier if your method is setup as:
- (void)someButtonHandler:(UIButton *)button {
}
这篇关于如何获得子视图在UITableViewCell中相对于整个窗口的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!