简体   繁体   English

检索旋转的UIImageView的所有4个坐标

[英]Retrieving all 4 coordinates of a rotated UIImageView

How does one get the 4 coordinates for a UIImageView? 如何获得UIImageView的4个坐标?

I know the CGRect can be obtained and the origin.x and origin.y, but how can all 4 corners be found? 我知道可以获得CGRect和origin.x和origin.y,但是如何找到所有4个角?

EDIT: I am rotating the UIImageViews, thats why I asked :P 编辑:我正在旋转UIImageViews,这就是为什么我问:P

You could add width and height of the rectangle to get the coordinates of the other 3 points. 您可以添加矩形的宽度和高度以获得其他3个点的坐标。

CGRect rect = view.bounds;

CGPoint topLeft = rect.origin;
CGPoint topRight = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y);
CGPoint bottomLeft =CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
CGPoint bottomRight = CGPointMake(rect.origin.x + rect.size.width,
                                  rect.origin.y + rect.size.height);

Then you could use CGPointApplyAffineTransform to get the transformed coordinates of them under your specified transform. 然后,您可以使用CGPointApplyAffineTransform在指定的变换下获取它们的变换坐标。

CGPoint center = view.center;
CGAffineTransform transf = CGAffineTransformMakeTranslation(-rect.size.width/2,
                                                            -rect.size.height/2);
transf = CGAffineTransformConcat(transf, view.transform);
transf = CGAffineTransformTranslate(transf, center.x, center.y);

topLeft = CGPointApplyAffineTransform(topLeft, transf);
//...

(note: not tested.) (注意:未经测试。)

This is my solution: 这是我的解决方案:

  • [self] is a subclass of UIImageView [self]UIImageView的子类
  • [self.transform] is the transform i make on [self] : [self.transform]是我在[self]上做的转换:

     CGAffineTransform transform = CGAffineTransformMakeTranslation(-center.x, -center.y); transform = CGAffineTransformConcat(transform, self.transform); CGAffineTransform transform1 = CGAffineTransformMakeTranslation(center.x, center.y); transform = CGAffineTransformConcat(transform, transform1); CGPoint leftTopPoint = CGPointApplyAffineTransform(leftTopPoint, transform); CGPoint rightTopPoint = CGPointApplyAffineTransform(rightTopPoint, transform); CGPoint rightBottomPoint = CGPointApplyAffineTransform(rightBottomPoint, transform); CGPoint leftBottomPoint = CGPointApplyAffineTransform(leftBottomPoint, transform); 

You can get the size.width and size.height. 你可以得到size.width和size.height。 Adding those to the x and y will give you the other coordinates. 将这些添加到x和y将为您提供其他坐标。

Whilst these are (of course) relative to the superview, you can use the frame property to obtain a CGRect containing the origin and size of the UIImageView. 虽然这些(当然)相对于superview,但您可以使用frame属性来获取包含UIImageView的原点和大小的CGRect You can then simply add the relevant size to the relevant origin point to obtain the full set of coordinates. 然后,您可以简单地将相关尺寸添加到相关原点以获得完整的坐标集。

See the frame section in the UIView class reference for more information. 有关更多信息,请参阅UIView类参考中的框架部分。

Construct a rotation matrix http://en.wikipedia.org/wiki/Rotation_matrix . 构建一个旋转矩阵http://en.wikipedia.org/wiki/Rotation_matrix You should calculate the initial positions of the corners relative to the point which is the center of rotation. 您应该计算角点相对于旋转中心点的初始位置。 Store those positions in an array and keep them all the time. 将这些位置存储在一个数组中并始终保留它们。 You calculate new positions by passing the angle in a 2x2 rotation matrix and multiplying them with initial positions. 您可以通过将角度传递到2x2旋转矩阵并将它们与初始位置相乘来计算新位置。

Well, given you know the angle of rotation, this is the maths to get the y coordinate of the top right corner: 好吧,如果你知道旋转角度,这是得到右上角y坐标的数学:

Sin (angle of rotation) = height difference y / width

Therefore if you're rotating the rectangle by 10 deg and it has a width of 20pt: 因此,如果您将矩形旋转10度并且宽度为20pt:

Sin 10 = yDiff / 20

Which means you can do this: 这意味着你可以这样做:

yDiff = Sin 10 * 20

This gives you the difference in y from the y coordinate of the origin to the y coordinate of the top right corner. 这使您可以看到y与原点的y坐标和右上角的y坐标之间的差异。 Add this value to the current y origin of your rectangle to get the actual y coordinate of your top right corner. 将此值添加到矩形的当前y原点以获取右上角的实际y坐标。 The next step is to use pythagoras on your width and the yDiff to get the xDiff and do the same (add it to the x coordinate) to get the x coordinate of your right hand corner. 下一步是在宽度和yDiff上使用毕达哥拉斯来获得xDiff并执行相同的操作(将其添加到x坐标)以获得右手角的x坐标。 I hope this makes sense. 我希望这是有道理的。

Now you just need to do it again for each other corner - imagine, if you will, that the rectangle has rotated through 90 deg, you can just reapply the logic, however x is y and vice versa. 现在你只需要为另一个角再做一次 - 想象一下,如果你愿意的话,矩形旋转90度,你可以重新应用逻辑,但x是y,反之亦然。 :) etc :)等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM