有没有人能推荐一个在iPhone MKMapView
中实现MKPinAnnotationView
拖拽功能的好教程
发布于 2012-12-24 13:44:57
您可以从this链接中找到MKPinAnnotationView拖放功能的源代码演示。
Github更新:上面站点url中给出的Github项目链接不起作用。但我在this url上找到了一个新的项目示例。
发布于 2012-12-24 13:15:27
要使批注可拖动,请将批注视图的draggable属性设置为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);
}
}
这应该会有帮助!
https://stackoverflow.com/questions/14017150
复制相似问题