iOS:在移动地图标注时显示信息气泡视图

5

我已经实现了与Uber iOS应用程序相同的功能,即根据路线移动图钉并进行动画。

问题:当我点击正在移动的图钉时,无法获取MKMapViewdidSelectAnnotationView委托。但是当图钉静止不动时,它会被调用。

代码:创建图钉

-(MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    else {

        NSString *annotationIdentifier = @"CustomViewAnnotation";

        AnnotationView * annotationView = (AnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];

        if(annotationView == nil) {

            annotationView = [[AnnotationView alloc] initWithAnnotation:annotation
                                                                        reuseIdentifier:annotationIdentifier];
            UIImage *pinIcon = [UIImage imageNamed:@"carIcon"];
            annotationView.btnInfo = [[UIButton alloc] init];
            annotationView.frame = CGRectMake(0.0f, 0.0f, pinIcon.size.width, pinIcon.size.height);
            annotationView.btnInfo.frame = annotationView.frame;
            [annotationView.btnInfo setBackgroundImage:pinIcon forState:UIControlStateNormal];
            [annotationView addSubview:annotationView.btnInfo];
            [annotationView.btnInfo setUserInteractionEnabled:NO];
        }

        else
            annotationView.annotation = annotation;

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

        return annotationView;
    }
}

更新坐标
- (void)updateLocation {

    CLLocationCoordinate2D oldLocation;
    CLLocationCoordinate2D newLocation;

    oldLocation.latitude = [self convertStringToFloat:self.arrayCSV[self.index-1][@"Latitude"]];
    oldLocation.longitude = [self convertStringToFloat:self.arrayCSV[self.index-1][@"Longitude"]];
    newLocation.latitude = [self convertStringToFloat:self.arrayCSV[self.index][@"Latitude"]];
    newLocation.longitude = [self convertStringToFloat:self.arrayCSV[self.index][@"Longitude"]];
    float getAngle = [self angleFromCoordinate:oldLocation toCoordinate:newLocation];

    CLLocation *oldInfo = [[CLLocation alloc] initWithLatitude:oldLocation.latitude longitude:oldLocation.longitude];
    CLLocation *newInfo = [[CLLocation alloc] initWithLatitude:newLocation.latitude longitude:newLocation.longitude];

    [UIView animateWithDuration:3
        animations:^{
            myAnnotation.coordinate = newLocation;
            AnnotationView *annotationView = (AnnotationView *)[self.mapView viewForAnnotation:myAnnotation];                
            annotationView.transform = CGAffineTransformMakeRotation(getAngle);
    }];
}
1个回答

2

试一下这个。

我认为你看到的行为是因为,默认情况下,在新块调用期间,用户交互被禁用了。

您可以通过像下面这样传递UIViewAnimationOptionAllowUserInteraction来覆盖此行为:

[UIView animateWithDuration:3 
    delay:0
    options:UIViewAnimationOptionAllowUserInteraction
    animations:^{
       myAnnotation.coordinate = newLocation;
       AnnotationView *annotationView = (AnnotationView *)[self.mapView viewForAnnotation:myAnnotation];                
       annotationView.transform = CGAffineTransformMakeRotation(getAngle);
     }
 completion:nil];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接