2012-04-19 93 views
1

我已成功更改MKAnnotationView以使用我的自定義圖像和下面的代碼片段。然而,我的問題是,一旦我啓用默認引腳動畫使用無法使用自定義圖像執行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

嘗試水木清華這樣的:

- (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; 
} 
+0

是的,你需要做的動畫你自。我沒有想到Offset,但它當然可以解決地圖和視圖的所有協調問題。 – 2012-04-19 15:21:19

1

如果有人想這樣做基於塊的方式,而不是開始/提交的方式(這是從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 
         } 
        }]; 

感謝您的回答妖,真正有用的