我正在使用SMCalloutView
link here为我的注释添加'UICalloutView'。它的工作原理是,当我点击注释时,会出现标注。我可以通过再次点击相同的注释或地图本身来解除标注。但是,在这种情况下会出现奇怪的行为;
我选择一个显示'SMCalloutView'的注释。然后我选择另一个注释(不点击原始注释或地图,以解除它),结果,原始的'SMCalloutView'消失,但新的注释出现,并立即消失(闪烁效果)。我按照这个例子,在github上没有大量的文档,但编码示例得到了很好的解释,这是我的代码;
MapViewController.m
@interface MapViewController ()<MKMapViewDelegate,CLLocationManagerDelegate,SMCalloutViewDelegate>
-(void)viewDidLoad{
//code omitted
// create our custom callout view
self.calloutView = [SMCalloutView platformCalloutView];
self.calloutView.delegate = self;
}
#pragma mark - MKMapView delegate methods
- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
NSString *annotationIdentifier = @"PinViewAnnotation";
SandwichAnnotationView *pinView = (SandwichAnnotationView *) [mv dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!pinView)
{
pinView = [[SandwichAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
pinView.canShowCallout = NO;
}
else
{
pinView.annotation = annotation;
pinView.canShowCallout=YES;
}
return pinView;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)annotationView {
if (mapView == self.map) {
// apply the MKAnnotationView's basic properties
self.calloutView.title = annotationView.annotation.title;
self.calloutView.subtitle = annotationView.annotation.subtitle;
// Apply the MKAnnotationView's desired calloutOffset (from the top-middle of the view)
self.calloutView.calloutOffset = annotationView.calloutOffset;
// create a disclosure button for comparison
UIButton *disclosure = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[disclosure addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disclosureTapped)]];
self.calloutView.rightAccessoryView = disclosure;
// iOS 7 only: Apply our view controller's edge insets to the allowable area in which the callout can be displayed.
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
self.calloutView.constrainedInsets = UIEdgeInsetsMake(self.topLayoutGuide.length, 0, self.bottomLayoutGuide.length, 0);
// This does all the magic.
[self.calloutView presentCalloutFromRect:annotationView.bounds inView:annotationView constrainedToView:self.map animated:YES];
}
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
[self.calloutView dismissCalloutAnimated:YES];
}