2011-05-03 114 views
2

我有MKPointAnnotation用以下方法,該方法我在iOS 4運行的子類:爲什麼UIView:animateWithDuration會立即完成?

- (void)animate { 
    [UIView 
    animateWithDuration: 3.0 
    delay:1.0 
    options:UIViewAnimationOptionAllowUserInteraction 
    animations:^{ 
     CLLocationCoordinate2D loc = 
      CLLocationCoordinate2DMake([self coordinate].latitude + 0.001, 
             [self coordinate].longitude + 0.001); 
     [self setCoordinate:loc]; 

    } 
    completion:^(BOOL finished) { 
     NSLog(@"Is completed"); 
    }]; 
} 

它通過點擊UIBarButtonItem調用。

我希望看到我的註釋在MKMapView之間移動。但是,所有我看到的是其最後的歸宿註釋當我打電話的方法是這樣的:

[mapView addAnnotation:myAnnotation]; 
[myAnnotation animate]; 
[myAnnotation release]; 

,如果我這樣調用此方法的預期動畫只發生:

[mapView addAnnotation:myAnnotation]; 
[myAnnotation performSelector:@selector(animate) withObject:nil afterDelay:0.5]; 
[myAnnotation release]; 

注意,我如果'afterDelay'小,例如,得到意想不到的行爲< 0.1s。

任何想法,爲什麼這可能是這種情況?

+1

座標屬性是否自動生成動畫?我沒有使用過MapKit,但並不是所有的屬性都是自動動畫的。 – Joe 2011-05-03 13:15:11

+0

我不認爲座標屬性本身是自動生成動畫的,但我相信'MKPointAnnotation'的實現必須連接到UIView的動畫屬性,因爲如果我使用'animate'方法'performSelector'具有足夠大的'afterDelay'值。 – Dan 2011-05-03 13:20:53

+0

當我使用傳統的非塊動畫時,我遇到同樣的問題。我仍然沒有找到這個問題的答案。 – Dan 2011-06-27 06:38:13

回答

1

動畫最終作用於MKAnnotationView,而不是MKAnnotation。將註釋添加到MKMapView並不一定意味着相應的視圖將很快添加。

因此,如果您嘗試在相應的MKAnnotationView已添加到MKMapView之前嘗試對MKAnnotation進行動畫處理,您會發瘋。

解決方法是,只有在知道其相應的MKAnnotationView已添加到MKView後,纔會動畫MKAnnotation。您可以使用MKMapViewDelegate- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views來觸發動畫。

相關問題