【发布时间】:2012-12-10 15:04:33
【问题描述】:
谁能推荐一个很好的教程来为 iPhone MKMapView 中的MKPinAnnotationView 实现拖动功能?
【问题讨论】:
-
重复:stackoverflow.com/questions/11927692/… 和许多其他人
标签: iphone ios mkmapview draggable mkpinannotationview
谁能推荐一个很好的教程来为 iPhone MKMapView 中的MKPinAnnotationView 实现拖动功能?
【问题讨论】:
标签: iphone ios mkmapview draggable mkpinannotationview
要使注释可拖动,请将注释视图的可拖动属性设置为 YES。
这通常在 viewForAnnotation 委托方法中完成,因此请确保将 MKMapView 委托设置为 self 并在 .h 文件中符合它。
例如:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *reuseId = @"pin";
MKPinAnnotationView *pav = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (pav == nil)
{
pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
pav.draggable = YES; // Right here baby!
pav.canShowCallout = YES;
}
else
{
pav.annotation = annotation;
}
return pav;
}
好的,下面是管理注释拖动操作的代码:
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
if (newState == MKAnnotationViewDragStateEnding) // you can check out some more states by looking at the docs
{
CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
NSLog(@"dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
}
}
这应该会有所帮助!
【讨论】: