我正在尝试实现一个带有自定义标注视图的自定义注释。我已经成功地将两者集成到我的应用程序中,如下所示:

我已经在一个xib文件中创建了我的自定义标注视图:

但是,当用户缩放/拖动地图时,我的标注视图停留在屏幕上的同一位置,而地图和注释位于新位置:

我想知道如何才能使标注视图保持在注释之上,即使地图被移动、旋转或拖动。我的标注视图的代码可以在下面找到:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil; //return nil to use default white dot view
}
if ([annotation isKindOfClass:[FishAnnotation class]])
{
static NSString *reuseId = @"seafoodRestaurant";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
}
else
{
annotationView.annotation = annotation;
}
FishAnnotation *ann = (FishAnnotation *)annotation;
annotationView.image = [UIImage imageNamed:ann.imageName];
annotationView.draggable = YES;
annotationView.canShowCallout = NO;
return annotationView;
}
//return nil (default view) if annotation is not our custom type
return nil;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
CalloutView *calloutView = (CalloutView *)[[[NSBundle mainBundle] loadNibNamed:@"calloutView" owner:self options:nil] objectAtIndex:0];
CGRect calloutViewFrame = calloutView.frame;
calloutViewFrame.origin = CGPointMake(view.frame.origin.x - calloutView.frame.size.width/2 + 18, view.frame.origin.y-calloutView.frame.size.height);
calloutView.frame = calloutViewFrame;
FishAnnotation *annotation = view.annotation;
[calloutView setTitle:@"Seafood Restaurant!" subTitle:@"647-123-4567"];
calloutView.annotation = view.annotation;
[view.superview addSubview:calloutView];
[view.superview bringSubviewToFront:calloutView];
}
-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
for (UIView *subview in view.superview.subviews)
{
if ([subview isKindOfClass:[CalloutView class]])
{
[subview removeFromSuperview];
}
}
}如果有办法将标注视图固定在注释上方,请让我知道。我试着用
[view addSubview:calloutView];
[view bringSubviewToFront:calloutView];但是,如果我尝试这样做,调用视图不会显示(它是隐藏的),而不是使用view.superview。
发布于 2014-07-16 04:55:15
我在下面找到了一个临时解决方案。这样做的问题是我的标注视图上的按钮不再起作用。当我选择一个按钮时,标注消失。
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
FishAnnotation *annotation = view.annotation;
// [appleMapView setCenterCoordinate:annotation.coordinate animated:YES];
if(![view.annotation isKindOfClass:[MKUserLocation class]])
{
CalloutView *calloutView = (CalloutView *)[[[NSBundle mainBundle] loadNibNamed:@"calloutView" owner:self options:nil] objectAtIndex:0];
CGRect calloutViewFrame = calloutView.frame;
calloutViewFrame.origin = CGPointMake(-calloutView.frame.size.width/2 + 18, -calloutView.frame.size.height);
calloutView.frame = calloutViewFrame;
[calloutView setTitle:annotation.title subTitle:annotation.subTitle];
calloutView.annotation = view.annotation;
[view addSubview:calloutView];
[view bringSubviewToFront:calloutView];
}
}
-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
for (UIView *subview in view.subviews)
{
if ([subview isKindOfClass:[CalloutView class]])
{
[subview removeFromSuperview];
}
}
}发布于 2014-07-16 12:57:52
我建议你使用这个库,因为我最近在我的项目中使用过它。
SMCalloutView https://github.com/nfarina/calloutview
它将帮助您处理mapView滚动和平移。
然后,我相信您可以加载customView和addSubView并将其放入SMCalloutView中。
https://stackoverflow.com/questions/24765876
复制相似问题