如何正确旋转地图上的叠加层?
我创建了一个自定义叠加层,其中包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的解决方案,在 swift 3 上运行:
假设
centerPoint
是您想要旋转的CGPoint
。Here is my solution, run on swift 3:
Assume
centerPoint
is aCGPoint
that you want to rotate around.您是否尝试过设置旋转的锚点?设置锚点后,旋转将围绕视图中心。
类似于:
请参阅 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:
See the Apple docs for more details.