繁体   English   中英

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

[英]Retrieving all 4 coordinates of a rotated UIImageView

如何获得UIImageView的4个坐标?

我知道可以获得CGRect和origin.x和origin.y,但是如何找到所有4个角?

编辑:我正在旋转UIImageViews,这就是为什么我问:P

您可以添加矩形的宽度和高度以获得其他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);

然后,您可以使用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);
//...

(注意:未经测试。)

这是我的解决方案:

  • [self]UIImageView的子类
  • [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); 

你可以得到size.width和size.height。 将这些添加到x和y将为您提供其他坐标。

虽然这些(当然)相对于superview,但您可以使用frame属性来获取包含UIImageView的原点和大小的CGRect 然后,您可以简单地将相关尺寸添加到相关原点以获得完整的坐标集。

有关更多信息,请参阅UIView类参考中的框架部分。

构建一个旋转矩阵http://en.wikipedia.org/wiki/Rotation_matrix 您应该计算角点相对于旋转中心点的初始位置。 将这些位置存储在一个数组中并始终保留它们。 您可以通过将角度传递到2x2旋转矩阵并将它们与初始位置相乘来计算新位置。

好吧,如果你知道旋转角度,这是得到右上角y坐标的数学:

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

因此,如果您将矩形旋转10度并且宽度为20pt:

Sin 10 = yDiff / 20

这意味着你可以这样做:

yDiff = Sin 10 * 20

这使您可以看到y与原点的y坐标和右上角的y坐标之间的差异。 将此值添加到矩形的当前y原点以获取右上角的实际y坐标。 下一步是在宽度和yDiff上使用毕达哥拉斯来获得xDiff并执行相同的操作(将其添加到x坐标)以获得右手角的x坐标。 我希望这是有道理的。

现在你只需要为另一个角再做一次 - 想象一下,如果你愿意的话,矩形旋转90度,你可以重新应用逻辑,但x是y,反之亦然。 :)等

暂无
暂无

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

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