【问题标题】:Co-ordinates of the four points of a uiview which has been rotated已旋转的uiview的四个点的坐标
【发布时间】:2011-12-24 17:27:47
【问题描述】:

这个有可能吗?如果是这样,谁能告诉我怎么做?

【问题讨论】:

  • 你可以使用一些基本的三角函数(sin 和 cosine、arcsin 和 arccos)来计算一个已经旋转的矩形的所有角的坐标。
  • 但目前我只有 origin.x 和 origin.y 值。只有这个我如何计算其他坐标?

标签: iphone objective-c cocoa-touch uiview xcode4


【解决方案1】:
CGPoint topLeft = view.bounds.origin;
topLeft = [[view superview] convertPoint:topLeft fromView:view];

CGPoint topRight = CGPointMake(view.bounds.origin.x + view.bounds.width, view.bounds.origin.y);
topRight = [[view superview] convertPoint:topRight fromView:view];

// ... likewise for the other points

第一个点在视图的坐标空间中,它总是“直立”的。然后下一条语句在父视图的坐标空间中找到该点对应的点。注意未转换的视图,这将等于view.frame.origin。上面的计算给出了转换后视图的 view.frame 角的等效值。

【讨论】:

  • 这行得通,虽然我不确定如何使用它来获取左下角坐标。此外,如果涉及图像,使用view.image.size.width 而不是view.bounds.width 更准确。
  • 刚刚算出左下坐标:CGPoint bottomRight = CGPointMake(self.dragView.bounds.origin.x + self.dragView.image.size.width, self.dragView.bounds.origin.y + self.dragView.image.size.height); bottomRight = [[self.dragView superview] convertPoint:bottomRight fromView:self.dragView];
【解决方案2】:
  • 获取视图框架的四个点 (view.frame)
  • 检索应用于您的视图的CGAffineTransform (view.transform)
  • 然后使用CGPointApplyAffineTransform(以及CGAffineTransform Reference 的同级方法)对四个点应用同样的仿射变换

【讨论】:

  • 请注意,我还没有测试过,但您可能需要应用翻译来制作变换的原点(这是您视图的锚点,如果您没有,可能默认为它的中心) t改变它)指向0,0,然后应用变换本身,并再次进行平移。可能会尝试两种情况来确定。不要忘记管理您正在使用的坐标系(即表示您的 CGPoint 坐标的参考/原点/坐标空间(可能是超级视图的边界坐标系,但以子视图的锚点为中心)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
  • 2014-02-26
  • 2016-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多