打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Using convertPoint to get the relative position inside a parent UIView
http://stackoverflow.com/questions/15883305/using-convertpoint-to-get-the-relative-position-inside-a-parent-uiview
12favorite4
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.
Imagine this setup:
I want to get the center coordinates of the UIButton relative to the 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?
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]]; // failI 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?
ios objective-c
shareimprove this question
asked Apr 8 '13 at 15:37
Axeva
1,71511543
It will help if you add the following to your question: The frames of all 4 views in your image, the output you get from the methods you have tried, and the value you actually want. – rmaddy Apr 8 '13 at 15:43
2 Answers
activeoldestvotes
26accepted
button.center is the center specified within the coordinate system of its superview, so I assume that the following works:
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];
shareimprove this answer
edited Apr 8 '13 at 15:51
answered Apr 8 '13 at 15:45
Martin R
188k15286364
1
Bingo! Your first attempt was what I was looking for. The center being tied to the coordinate system of the superview is what I was missing. Thank you! – Axeva Apr 8 '13 at 16:10
4
Martin answer is correct. For developers using Swift, you can get the position of an object (button, view,...) relative to the screen by using:
var p = obj.convertPoint(obj.center, toView: self.view)println(p.x) // this prints the x coordinate of 'obj' relative to the screenprintln(p.y) // this prints the y coordinate of 'obj' relative to the screen
shareimprove this answer
edited Jul 27 at 3:59
answered Nov 19 '14 at 7:01
RSC
1,732812
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
iOS事件分发机制(一) hit
关于UIView的autoresizingMask属性的研究
UIView 的 autoresizingMask 属性 详解。
IOS-- UIView中的坐标转换
两个View翻转显示
自定义 push 和 pop 实现有趣的相册翻开效果(上)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服