如何正确旋转地图上的叠加层?

发布于 2024-11-07 06:17:08 字数 426 浏览 4 评论 0原文

我创建了一个自定义叠加层,其中包含 iOS 应用程序的几何形状。我想围绕它的中心(或者更确切地说,boundingMapRect 的中心)旋转它。据我了解,使用 CGContextRotateCTM(contextRef, angle) 使用boundingMapRect的左上角作为原点旋转叠加层,因此如果我顺时针旋转叠加层少量,它会出现在其先前位置的下方和左侧。

我认为如果我可以捕获叠加层边界矩形的初始中心,然后捕获旋转后的中心,那么我可以计算坐标差并使用 CGContextTranslateCTM 重新定位叠加层。我的问题是,CGContextRotateCTM 调用之前和之后的中心位置是相同的。有什么方法可以强制该调用生效,以便我可以获得新的中心位置?有更好的方法吗?

我看到一些关于使用覆盖层宽度和高度的一半来重新定位的参考,但我不明白这有什么帮助,因为它是恒定的。

谢谢。

I've created a custom overlay that contains a geometric shape for an iOS app. I would like to rotate it around it's center (or rather the center of the boundingMapRect). From what I understand, using CGContextRotateCTM(contextRef, angle) rotates the overlay using the upper left corner of the boundingMapRect as the origin, so if I rotate my overlay a small amount clockwise it appears down and to the left of it's previous location.

I thought that if I could capture the initial center of the overlay's bounding rectangle, then the center after the rotation then I could calculate the coordinate difference and reposition the overlay using CGContextTranslateCTM. My problem with that is that the center locations were the same before and after the CGContextRotateCTM call. Is there some way to force that call into effect so I can get the new center location? Is there a better way to do this?

I saw some references to using half the width and height of the overlay to reposition, but I don't understand how that helps as it is constant.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苹果你个爱泡泡 2024-11-14 06:17:08

这是我的解决方案,在 swift 3 上运行:

假设 centerPoint 是您想要旋转的 CGPoint

let a = sqrt(pow(centerPoint.x, 2.0) + pow(centerPoint.y, 2.0))

let sub1 = (centerPoint.y / a) * cos(angle / 2.0)
let sub2 = (centerPoint.x / a) * sin(angle / 2.0)
let deltaX = -2 * a * sin((0 - angle) / 2.0) * (sub1 + sub2)

let sub3 = (centerPoint.x / a) * cos(angle / 2.0)
let sub4 = (centerPoint.y / a) * sin(angle / 2.0)
let deltaY = 2 * a * sin((0 - angle) / 2.0) * (sub3 - sub4)

context.translateBy(x: deltaX, y: deltaY)

context.rotate(by: angle)

Here is my solution, run on swift 3:

Assume centerPoint is a CGPoint that you want to rotate around.

let a = sqrt(pow(centerPoint.x, 2.0) + pow(centerPoint.y, 2.0))

let sub1 = (centerPoint.y / a) * cos(angle / 2.0)
let sub2 = (centerPoint.x / a) * sin(angle / 2.0)
let deltaX = -2 * a * sin((0 - angle) / 2.0) * (sub1 + sub2)

let sub3 = (centerPoint.x / a) * cos(angle / 2.0)
let sub4 = (centerPoint.y / a) * sin(angle / 2.0)
let deltaY = 2 * a * sin((0 - angle) / 2.0) * (sub3 - sub4)

context.translateBy(x: deltaX, y: deltaY)

context.rotate(by: angle)
我爱人 2024-11-14 06:17:08

您是否尝试过设置旋转的锚点?设置锚点后,旋转将围绕视图中心。

类似于:

[yourView.layer setAnchorPoint:CGPointMake( 0.5, 0.5 )]; // center of the view

请参阅 Apple 文档 了解更多详细信息。

Have you tried setting an anchor point for the rotation? After setting the anchorpoint the rotation will be around the center of the view.

Somehting like:

[yourView.layer setAnchorPoint:CGPointMake( 0.5, 0.5 )]; // center of the view

See the Apple docs for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文