Rotate MKAnnotationPinView according to the device heading

笑着哭i 提交于 2019-12-24 13:26:14

问题


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.



来源:https://stackoverflow.com/questions/12763469/rotate-mkannotationpinview-according-to-the-device-heading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!