无法在第二次点击MKPinAnnotationView

时间:2017-09-11 05:15:57

标签: ios annotations mapkit mkannotationview

我已使用此代码创建了自定义注释。

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
    {
        static NSString *const AnnotatioViewReuseID = @"AnnotatioViewReuseID";
       // MKAnnotationView

        MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotatioViewReuseID];

        if (!annotationView) {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotatioViewReuseID];
        }

        if ([annotation isKindOfClass:[FBAnnotationCluster class]]) {
            FBAnnotationCluster *cluster = (FBAnnotationCluster *)annotation;
            cluster.title = [NSString stringWithFormat:@"%lu", (unsigned long)cluster.annotations.count];

            UIView *view = [[UIView alloc]init];
            view.backgroundColor = [UIColor colorWithRed:33.0/255.0 green:191.0/255.0 blue:133.0/255.0 alpha:1.0];

            UILabel *label = [[UILabel alloc]init];
            label.text = cluster.title;
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor whiteColor];
            label.textAlignment = NSTextAlignmentCenter;
            UIFont *font = [UIFont fontWithName:@"Avenir-Medium" size:14.0];
            label.font = font;

            label.frame = CGRectMake(0, 0, [self widthOfString:label.text]+20, [self widthOfString:label.text]+20);

            view.frame = CGRectMake(0, 0, label.frame.size.width, label.frame.size.width) ;

            view.layer.cornerRadius =  view.frame.size.height/2;
            view.layer.borderColor = [UIColor whiteColor].CGColor;
            view.layer.borderWidth = 2.0;

            view.clipsToBounds = true;

            [view addSubview:label];

            for (UIView *view in [annotationView subviews])
            {
                [view removeFromSuperview];
            }
            [annotationView addSubview:view];

            annotationView.enabled = YES;
            annotationView.annotation = annotation;
            annotationView.canShowCallout = YES;

            annotationView.pinTintColor = [UIColor clearColor];

        } else {

            annotationView.pinTintColor = [UIColor clearColor];

            annotationView.layer.borderColor = [UIColor clearColor].CGColor;
            annotationView.layer.borderWidth = 0.0;

            FBAnnotation *a = (FBAnnotation*)annotation;

            NSLog(@"amount is %f",a.amount);

            UIImage *image = [UIImage imageNamed:@"icon-marker-select"];

            UIImageView *imgView = [[UIImageView alloc]init];

            NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
            formatter.numberStyle = NSNumberFormatterDecimalStyle;
            formatter.maximumFractionDigits = 2;

            NSString *result = [formatter stringFromNumber:[NSNumber numberWithDouble:a.amount]];

            NSString *strData = [NSString stringWithFormat:@"%@%@",a.currency,result];
            UIFont *font = [UIFont fontWithName:@"Avenir-Medium" size:14.0];

            NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
            paragraphStyle.alignment = NSTextAlignmentCenter;
            NSDictionary *attributes = @{
                                         NSFontAttributeName : font,
                                         NSParagraphStyleAttributeName : paragraphStyle,
                                         NSForegroundColorAttributeName : [UIColor whiteColor]
                                         };
            CGSize textSize = [strData sizeWithAttributes:attributes];

            CGRect textRect = CGRectMake(5, (image.size.height-textSize.height)/2 - 2, textSize.width , textSize.height);

            UILabel *textLable = [[UILabel alloc]initWithFrame:textRect];

            textLable.textColor = [UIColor whiteColor];
            textLable.font = font;
            textLable.text = strData;

            UIImage *lightSymImg = [UIImage imageNamed:@"icon_lightning"];

           UIImageView *lightImage = [[UIImageView alloc]init];

            lightImage.image = lightSymImg;

            CGRect imgRect = CGRectMake(textRect.origin.x+textRect.size.width, (image.size.height-lightSymImg.size.height)/2 - 2, lightSymImg.size.width,lightSymImg.size.height);

            lightImage.frame = imgRect;

            imgView.frame = CGRectMake(0, 0, imgRect.size.width + imgRect.origin.x+5, image.size.height);

            imgView.image = image;

            for (UIView *view in [annotationView subviews])
            {
                [view removeFromSuperview];
            }
            annotationView.annotation = annotation;

            [annotationView addSubview:imgView];
            [annotationView addSubview:textLable];
            [annotationView addSubview:lightImage];

            annotationView.enabled = YES;
            annotationView.canShowCallout = NO;
        }
        return annotationView;
        }

在didSelect方法中,我已编写此代码用于呈现视图控制器

 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
    {
        NSLog(@"didSelectAnnotationView");

        if ([view.annotation isKindOfClass:[FBAnnotationCluster class]]) {

            NSLog(@"FBAnnotationCluster select annotation");
        }
        else{


            SpotDetailVC *locDetailVC = [[UIStoryboard storyboardWithName:@"SpotDetail" bundle:nil] instantiateViewControllerWithIdentifier:@"SpotDetailVC"];

            [self presentViewController:navController animated:YES completion:nil];
        }
    }

现在,当我第一次单击注释时,它可以工作但在再次关闭该视图后单击相同的注释它不起作用。

请帮助,我知道这是愚蠢的错误,但我无法配置那个。

0 个答案:

没有答案