2011-01-30 60 views
2

蘋果的文檔告訴你這種方法應該儘可能的輕量化,這裏的標準用法是什麼?重置註釋引腳?我是否必須在我的MKMapview委託上實現mapView:regionWillChangeAnimated:?

告訴代表由地圖視圖中顯示的區域 即將 變化。

-(無效)的MapView:(的MKMapView *)的MapView regionWillChangeAnimated:(BOOL)動畫

參數

MapView的
地圖視圖,其可見光區是 即將改變。

動畫
如果是,換到新的區域 將動畫。如果否,則立即更改 。

只要 當前顯示的地圖區域 發生更改,就會調用此方法。在滾動期間,可能會多次調用此方法 以報告地圖位置的更新 。 因此,您執行此 方法時應儘可能輕量級,以避免影響滾動 的性能。

回答

0

與此委託方法的問題是「在滾動,這種方法可稱爲多次上報更新的地圖位置」(所以你需要IF/THEN或CASE/BREAK等,以保持它的「輕量級「)。你不需要使用這個方法(不是必需的),但是如果你想要合併某種功能(例如去除無用的引腳等),那麼保持它輕量級的示例代碼將是:

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{ 
if(!animated){ 
//Instantaneous change, which means you probably did something code-wise, so you should have handled anything there, but you can do it here as well. 

} else { 
//User is most likely scrolling, so the best way to do things here is check if the new region is significantly (by whatever standard) away from the starting region 

CLLocationDistance *distance = [mapView.centerCoordinate distanceFromLocation:originalCoordinate]; 
if(distance > 1000){ 
//The map region was shifted by 1000 meters 
//Remove annotations outsides the view, or whatever 
//Most likely, instead of checking for a distance change, you might want to check for a change relative to the view size 
} 

} 

} 
+0

哦所以註釋視圖不會自動糾正其位置? – quantumpotato 2011-01-31 13:31:28

相關問題