How to rotate the overlay on the map?

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

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

I have seen some references to using half width and overlay height for permutations, but I don’t understand how this helps, as it is permanent.

Thank.

+3
source share
2 answers

Here is my solution, run on fast 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)
+1

? anchorpoint .

- :

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

. Apple.

0

All Articles