ios 使用 convertPoint 获取父 UIView 内的相对位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15883305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using convertPoint to get the relative position inside a parent UIView
提问by Axeva
I've looked at a dozen SO questions on this topic, and none of the answers have worked for me. Maybe this will help get me back on the right path.
我已经查看了有关此主题的十几个 SO 问题,但没有一个答案对我有用。也许这会帮助我回到正确的道路上。
Imagine this setup:
想象一下这个设置:
I want to get the center
coordinates of the UIButton relative to the UIView.
我想获取center
UIButton 相对于 UIView的坐标。
In other words, the UIButton center may be 215, 80 within the UITableViewCell, but relative to the UIView they should be more like 260, 165. How do I convert between the two?
换句话说,UIButton 中心在 UITableViewCell 中可能是 215、80,但相对于 UIView,它们应该更像是 260、165。我如何在两者之间转换?
Here's what I've tried:
这是我尝试过的:
[[self.view superview] convertPoint:button.center fromView:button]; // fail
[button convertPoint:button.center toView:self.view]; // fail
[button convertPoint:button.center toView:nil]; // fail
[button convertPoint:button.center toView:[[UIApplication sharedApplication] keyWindow]]; // fail
I could do it the hard way by looping through all of the button's superviews and adding up the x and y coordinates, but I suspect that's overkill. I just need to find the right combination of covertPoint settings. Right?
我可以通过遍历所有按钮的超级视图并将 x 和 y 坐标相加来实现这一点,但我怀疑这太过分了。我只需要找到 covertPoint 设置的正确组合。对?
回答by Martin R
button.center
is the center specified within the coordinate system of its superview, so I
assume that the following works:
button.center
是在其 superview的坐标系内指定的中心,所以我假设以下工作:
CGPoint p = [button.superview convertPoint:button.center toView:self.view]
Or you compute the button's center in its own coordinate system and use that:
或者您在其自己的坐标系中计算按钮的中心并使用它:
CGPoint buttonCenter = CGPointMake(button.bounds.origin.x + button.bounds.size.width/2,
button.bounds.origin.y + button.bounds.size.height/2);
CGPoint p = [button convertPoint:buttonCenter toView:self.view];
Swift 4
斯威夫特 4
var p = button.convert(button.center, to: self.view)
回答by rsc
Martin answer is correct. For developers using Swift, you can get the position of an object (button, view,...) relative to the screen by using:
马丁的回答是正确的。对于使用 Swift 的开发人员,您可以使用以下方法获取对象(按钮、视图等)相对于屏幕的位置:
var p = obj.convertPoint(obj.center, toView: self.view)
println(p.x) // this prints the x coordinate of 'obj' relative to the screen
println(p.y) // this prints the y coordinate of 'obj' relative to the screen
回答by Trev14
Swift 5.2
You need to call convert
from the button, not the superview. In my case I needed width data so I converted the bounds instead of just center point. The code below works for me:
Swift 5.2
您需要convert
从按钮调用,而不是从超级视图调用。在我的情况下,我需要宽度数据,所以我转换了边界而不是中心点。下面的代码对我有用:
let buttonAbsoluteFrame = button.convert(button.bounds, to: self.view)
回答by AamirR
Here is Swift 3update of @Pablo's answer, which off course worked great in my case.
这是@Pablo答案的Swift 3更新,当然对我来说效果很好。
if let window = UIApplication.shared.keyWindow {
parent.convert(child.frame.origin, to: window)
}
回答by Pablo Ruan
in swift 2.2 worked for me:
在 swift 2.2 中为我工作:
var OrignTxtNomeCliente:CGPoint!
if let orign = TXT_NomeCliente.superview, let win = UIApplication.sharedApplication().keyWindow {
OrignTxtNomeCliente = orign.convertPoint(TXT_NomeCliente.frame.origin, toView: win)
}