嗨,我正在开发地图应用程序。我正在旋转 mapview 和 imageview,它是 annotationView 上的子视图。Mapview 旋转是完美的,但是 imageview 并没有按照 Mapview 旋转,并且总是指向北方向。
我正在使用以下代码来旋转 imageview :
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *identifier = @"annView";
MKPinAnnotationView *annView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
annView=nil;
[annView setSelected:YES animated:NO];
if (annView == nil)
{
annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
}
MyAnnotation *myAnnotation=(MyAnnotation *)annotation;
NSString *details;
if ([myAnnotation isKindOfClass:[MKUserLocation class]])
{
//return nil;
}
else
{
details=[myAnnotation annType];
}
NSLog(@"Details is %@",details);
annView.canShowCallout = YES;
if([details isEqualToString:@"Current"])
{
[[annView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
annView.animatesDrop=NO;
annView.canShowCallout=YES;//pin3.png// Parking_Psymbol//dus2
annView.image=[UIImage imageNamed:@""];
UIImage * locaterImage;
locaterImage = [UIImage imageNamed:@"Locatermap.png"];
locaterImageView = [[UIImageView alloc] initWithImage:locaterImage];
locaterImageView.frame = CGRectMake(-28, -4, 70, 70);
//--------For animating imageview ---------------------
// Setup the animation
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationCurve:2.0];
//[UIView setAnimationRepeatCount:10];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
//--------For animating imageview ---------------------
[annView addSubview:locaterImageView];
}
对于旋转 MapView :
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
// Convert Degree to Radian and move the needle
float oldRad = -manager.heading.trueHeading * M_PI / 180.0f;
float newRad = -newHeading.trueHeading * M_PI / 180.0f;
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
theAnimation.fromValue = [NSNumber numberWithFloat:oldRad];
theAnimation.toValue=[NSNumber numberWithFloat:newRad];
theAnimation.duration = 0.5f;
[mapView.layer addAnimation:theAnimation forKey:@"transform.rotation"];
self.mapView.transform = CGAffineTransformMakeRotation(newRad);
}
我用谷歌搜索,但我找不到我需要的答案。请给出解决此问题的任何想法。
提前致谢。