启用和禁用注释拖动(即时)(iOS Mapkit) [英] Enabling and Disabling Annotation Dragging (On The Fly) (iOS Mapkit)

查看:85
本文介绍了启用和禁用注释拖动(即时)(iOS Mapkit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个mapview,它具有一个按钮,可根据项目要求在编辑"模式和拖动"模式之间切换.我意识到,通过在viewForAnnotation中将它们设置为可拖动,可以很容易地将您的注释从创建中拖出,但是所需的行为不允许这样做.我尝试了几种将注释更改为可拖动但未成功的方法.首先想到的是遍历现有注释并将每个注释设置为可拖动"和选定",但是我收到了一个无法识别的选择器发送给实例错误(我尝试实例化一个新注释以传递对象并重新绘制虽然在循环中,但我也收到相同的错误):

I've created a mapview which has a button to switch between an 'edit' mode and a 'drag' mode based on the project requirements. I realize that it's easy enough to have your annotations draggable from creation by setting them draggable in the viewForAnnotation, but the behavior required won't allow this. I've tried a couple different ways of changing the annotations to draggable without success. The first thought was to loop through the existing annotations and set each one to 'draggable' and 'selected', but I get an unrecognized selector sent to instance error (I did try instantiating a new annotation to pass in the object and re-plot while in the loop, but I get the same error as well):

NSLog(@"Array Size: %@", [NSString stringWithFormat:@"%i", [mapView.annotations count]]);

    for(int index = 0; index < [mapView.annotations count]; index++) {

        if([[mapView.annotations objectAtIndex:index]isKindOfClass:[locAnno class]]){
            NSLog(@"** Location Annotation at Index: %@", [NSString stringWithFormat:@"%i", index]);
            NSLog(@"* Location Marker: %@", [mapView.annotations objectAtIndex:index]);
        }

        if([[mapView.annotations objectAtIndex:index]isKindOfClass:[hydAnno class]]) {
            NSLog(@"** Hydrant Annotation at Index: %@", [NSString stringWithFormat:@"%i", index]);
            NSLog(@"* Hydrant Marker: %@", [mapView.annotations objectAtIndex:index]);

            [[mapView.annotations objectAtIndex:index]setSelected:YES];
            [[mapView.annotations objectAtIndex:index]setDraggable:YES];
        }
    }

第二个想法是使用'didSelectAnnotationView',并在选定注释时将其设置为选定状态并在其上拖动,并在模式再次切换回时重置属性.此方法有效,但效果很差,因为该事件并不总是会触发,并且您在更改属性之前先左击该注释一次或多次:

The second thought was to use 'didSelectAnnotationView', and set selected and draggable on the annotation when it's selected, and reset the properties when the mode switches back again. This works, but very poorly as the event doesn't always fire and your left to tap the annotation one or more times before it will change the properties:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
NSLog(@"Annotation Selected!");
if(!editMode) {
    view.selected = YES;
    view.draggable = YES;
}

}

如果我可以尝试的话,第一次尝试似乎是最简单的解决方案.另一方面,使用didSelect方法既麻烦又hack-licious.我对iOS开发非常陌生,因此如果对此有所遗忘,而我却忽视了新手,我深表歉意.我感谢社区可以提供的任何见解.非常感谢.

The first attempt seems the most simple solution if I can get it to work. Using the didSelect method on the other hand is cumbersome and hack-licious. I'm quite new to iOS development, so I apologize if I've overlooked something novice while hammering away at this. I appreciate any insight the community can offer. Thanks much.

推荐答案

第一种方法比使用didSelectAnnotationView委托方法更好.

The first method is better than using the didSelectAnnotationView delegate method.

导致无法识别的选择器"错误的代码存在问题,是它在注解对象(类型为id<MKAnnotation>)而不是其对应的MKAnnotationView对象上调用setSelected:setDraggable:. id<MKAnnotation>对象没有这种方法,因此您会遇到无法识别的选择器"错误.

The problem with the code which causes the "unrecognized selector" error is that it is calling setSelected: and setDraggable: on the annotation objects (type id<MKAnnotation>) instead of their corresponding MKAnnotationView objects. The id<MKAnnotation> objects don't have such methods so you get that "unrecognized selector" error.

地图视图的annotations数组包含对id<MKAnnotation>(数据模型)对象的引用-而不是这些注释的MKAnnotationView对象.

The map view's annotations array contains references to the id<MKAnnotation> (data model) objects -- not the MKAnnotationView objects for those annotations.

因此,您需要更改此内容:

So you need to change this:

[[mapView.annotations objectAtIndex:index]setSelected:YES];
[[mapView.annotations objectAtIndex:index]setDraggable:YES];

类似这样:

//Declare a short-named local var to refer to the current annotation...
id<MKAnnotation> ann = [mapView.annotations objectAtIndex:index];

//MKAnnotationView has a "selected" property but the docs say not to set
//it directly.  Instead, call deselectAnnotation on the annotation...
[mapView deselectAnnotation:ann animated:NO];

//To update the draggable property on the annotation view, get the 
//annotation's current view using the viewForAnnotation method...
MKAnnotationView *av = [mapView viewForAnnotation:ann];
av.draggable = editMode;


您还必须更新viewForAnnotation delegate 方法中的代码,以便它还将draggable设置为editMode而不是硬编码的YESNO,以便视图已经需要在for循环中更新后,需要重新为注释 创建视图,注释视图将具有正确的draggable值.


You must also update the code in the viewForAnnotation delegate method so that it also sets draggable to editMode instead of a hard-coded YES or NO so that if the map view needs to re-create the view for the annotation after you've already updated it in the for-loop, the annotation view will have the right value for draggable.

这篇关于启用和禁用注释拖动(即时)(iOS Mapkit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆