Rotate MKAnnotationPinView according to the device

2019-08-04 13:43发布

问题:

In my app I'm rotating the MapView according to the device heading, I have only one annotation and i'm rotating it's pin and it's pinView too. My rotation method is inside the next function

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
if (newHeading.headingAccuracy < 0)
    return;

CLLocationDirection  theHeading = ((newHeading.trueHeading > 0) ?
                                   newHeading.trueHeading : newHeading.magneticHeading);

lastHeading = theHeading;
[self rotateImage:self.mapView duration:0.1 degrees:-theHeading];

[self rotateImage:jerusalemPinView duration:0.1 degrees:theHeading];

}    

Rotation function:

- (void)rotateImage:(UIView *)view duration:(NSTimeInterval)duration
        degrees:(double)angleDegrees
{

// Setup the animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationBeginsFromCurrentState:YES];

// The transform matrix

CGAffineTransform transform =
CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(angleDegrees));
view.transform = transform;

[UIView commitAnimations];
}

it's working but then annotation and pinView are always "try to turn back to the north", then the next time when the device is being turned around my method works and so on.. What am i missing?

Thanks

回答1:

After rotating jerusalemPinView, does the view get redrawn by viewForAnnotation? I suggest putting a debug line at the top of the important methods and checking what order they are getting called in. My guess is that in viewForAnnotation you make a new jerusalemPinView, assign it to the pin, then didUpdateHeading gets called and your rotate it, then viewForAnnotation gets called again and a new (unrotated) jerusalemPinView is made again.