1

次のコード スニペットでカスタム イメージを使用するように MKAnnotationView を正常に変更しました。ただし、私の問題は、デフォルトのピンアニメーションを有効にすると

   annotationView.animatesDrop = YES; 

カスタム イメージはなくなり、デフォルトの赤いピンが使用されます。これを修正する方法はありますか?ありがとう

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MyAnnotation  *)annotation
 {
    if  ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;
// try to dequeue an existing pin view first
static NSString* identifier = @"AnnotationIdentifier";

MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
    annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    //!!!!!!!annotationView.animatesDrop = YES;!!!!!!!!!!
} else {
    annotationView.annotation = annotation;
}

  annotationView.image = [UIImage imageNamed:@"bla.png"];
 return annotationView;
}
4

2 に答える 2

4

次のように試してみてください:

- (void)loadView {
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    self.view.backgroundColor = [UIColor grayColor];
    [self.view setClipsToBounds:YES];

    MKMapView *mapView = [[MKMapView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, self.view.frame.size.height - 20)];
    mapView.delegate = self;
    [mapView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
    mapView.mapType = MKMapTypeStandard;
    [self.view addSubview:mapView];
}

#pragma mark - MKMapView Delegate Implementation

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {   
    for (MKAnnotationView *pin in views) {
        //UIButton *detailsButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        //[detailsButton addTarget:self action:@selector(detailsButtonAct:) forControlEvents:UIControlEventTouchUpInside];

        pin.canShowCallout = YES;
        //pin.rightCalloutAccessoryView = detailsButton;
        CGRect endFrame = pin.frame;
        pin.frame = CGRectOffset(pin.frame, 0, -230);

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.45f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

        pin.frame = endFrame;

        [UIView commitAnimations];
    }
}
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation {
    static NSString *AnnotationViewID = @"annotationViewID";
    MKAnnotationView *annotationView = (MKAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];

    annotationView.image = [UIImage imageNamed:@"map_pin.png"];
    annotationView.annotation = annotation;

    [annotationView addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:@"pinSelected"];
    [annotationView setEnabled:YES];

    return annotationView;
}
于 2012-04-19T11:14:25.860 に答える
1

誰かがこれをbegin/commit方法(IOS 4以降で推奨)ではなくブロックベースの方法で実行したい場合:

...
[UIView animateWithDuration:0.5f
                          delay:0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
                         pin.frame = endFrame;
                     }
                     completion:^(BOOL finished){
                         if (finished) {
                             // Do some action upon completion of animation
                         }
                     }];

答えの悪魔をありがとう、本当に役に立ちました

于 2013-01-25T17:13:25.187 に答える