5

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

问题:didSelectAnnotationView当我在移动时单击图钉时,我无法获得MKMapView. 但是当引脚稳定意味着不动时,它就会被调用。

代码:创建引脚

-(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);
    }];
}
4

1 回答 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];
于 2017-01-19T09:38:50.047 回答