Pie*_*ter 8 mkpinannotationview mkmapview mkannotationview ios
MKPinAnnotationView不允许您将自定义图像用作"pin"并同时启用拖动,因为一旦开始拖动,图像将更改回默认图钉.因此,我使用MKAnnotationView而不是MKPinAnnotationView.
虽然使用MKAnnotationView而不是MKPinAnnotationView会将您的自定义图像显示为"pin",但它不支持使用默认引脚获得的拖放动画.
无论如何,我的问题是,在我将自定义MKAnnotationView拖到地图上的新点然后移动地图本身之后,MKAnnotationView不再随地图一起移动.
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
static NSString *defaultID = @"myLocation";
if([self.annotation isKindOfClass:[PinAnnotation class]])
{
//Try to get an unused annotation, similar to uitableviewcells
MKAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:defaultID];
//If one isn't available, create a new one
if(!annotationView)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:self.annotation reuseIdentifier:defaultID];
annotationView.canShowCallout = YES;
annotationView.draggable = YES;
annotationView.enabled = YES;
}
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 32, 32)];
imgView.image = self.passableTag.image;
annotationView.leftCalloutAccessoryView = imgView;
annotationView.image = [UIImage imageNamed:[Constants tagIconImageNameForTagType:self.passableTag.type]];
return annotationView;
}
return nil;
}
Run Code Online (Sandbox Code Playgroud)
小智 15
在结束拖动后,只需将annotationView.dragState设置为MKAnnotationViewDragStateNone:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {
if ([view.annotation isKindOfClass:[PinAnnotation class]]) {
if (newState == MKAnnotationViewDragStateEnding) {
view.dragState = MKAnnotationViewDragStateNone;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4362 次 |
| 最近记录: |