问题描述
我想以编程方式获取 UIImageView 的坐标.我该怎么做?
I would like to get the coordinates of an UIImageView programmatically. How do I do this?
例如,我有一个正方形.我想得到所有角落的坐标.我该如何进行?
For example I have a square. I want to get the coordinates of all the corners. How must i proceed?
NSLog("%f", ImageView.frame.origin.x);
NSLog("%f", ImageView.frame.origin.y);
我得到了正方形的左上角坐标.
I get the topleft coordinate of the square.
我必须说正方形(图像视图)旋转,这就是为什么我必须得到它的坐标.
I must say that the square (imageview) rotates and that's why I must get it's coordinates.
推荐答案
坐标在谁的坐标空间?
每个 UIView 都有自己的坐标空间.您可以通过询问 bounds
来引用视图在其自己的坐标空间中的大小.
Each UIView has its own coordinate space. You can refer to a view's size in its own coordinate space by asking for its bounds
.
视图在其父视图中的大小和位置称为其frame
.在您的示例中,您要求图像视图在其父视图的坐标空间中的左上角.
A view's size and position in its parent view is called its frame
. In your example, you're asking for the image view's top left corner in its parent view's coordinate space.
如果你想这样做,那么试试这些:
If that's what you want to do, then try these:
frame.origin.x
frame.origin.y
frame.size.width
frame.size.height
通过将它们加在一起,您可以获得任何坐标:例如,右上角的 x 坐标将是
By adding those together you can get any coordinate: for example, the x coordinate of the top right would be
frame.origin.x + frame.size.width
这篇关于我们如何以编程方式获取 UIImageView 的坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!