菜鸟教程小白 发表于 2022-12-13 12:51:21

ios - 启用和禁用注释拖动(动态)(iOS Mapkit)


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

<pre><code>NSLog(@&#34;Array Size: %@&#34;, ]);

    for(int index = 0; index &lt; ; index++) {

      if([isKindOfClass:]){
            NSLog(@&#34;** Location Annotation at Index: %@&#34;, );
            NSLog(@&#34;* Location Marker: %@&#34;, );
      }

      if([isKindOfClass:]) {
            NSLog(@&#34;** Hydrant Annotation at Index: %@&#34;, );
            NSLog(@&#34;* Hydrant Marker: %@&#34;, );

            [setSelected:YES];
            [setDraggable:YES];
      }
    }
</code></pre>

<p>第二个想法是使用'didSelectAnnotationView',并在注释被选中时设置选定和可拖动,并在模式再次切换回来时重置属性。这很有效,但效果很差,因为事件并不总是触发,并且您的左键点击注释一次或多次,然后它会更改属性:</p>

<pre><code>- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
NSLog(@&#34;Annotation Selected!&#34;);
if(!editMode) {
    view.selected = YES;
    view.draggable = YES;
}
</code></pre>

<p>}</p>

<p>如果我能让它工作,第一次尝试似乎是最简单的解决方案。另一方面,使用 didSelect 方法既麻烦又难搞。我对 iOS 开发很陌生,所以如果我在讨论这个问题时忽略了一些新手,我深表歉意。我感谢社区可以提供的任何见解。非常感谢。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>第一种方法比使用<code>didSelectAnnotationView</code>委托(delegate)方法好。</p>

<p>导致“无法识别的选择器”错误的代码的问题是它在注释对象上调用 <code>setSelected:</code> 和 <code>setDraggable:</code> <MKAnnotation></code>) 而不是它们对应的 <code>MKAnnotationView</code> 对象。 <code>id<MKAnnotation></code> 对象没有这样的方法,因此您会收到“无法识别的选择器”错误。</p>

<p> mapView 的 <code>annotations</code> 数组包含对 <code>id<MKAnnotation></code> (数据模型)对象的引用——而不是那些的 <code>MKAnnotationView</code> 对象注释。</p>

<p>所以你需要改变这个:</p>

<pre><code>[setSelected:YES];
[setDraggable:YES];
</code></pre>

<p>到这样的事情:</p>

<pre><code>//Declare a short-named local var to refer to the current annotation...
id&lt;MKAnnotation&gt; ann = ;

//MKAnnotationView has a &#34;selected&#34; property but the docs say not to set
//it directly.Instead, call deselectAnnotation on the annotation...
;

//To update the draggable property on the annotation view, get the
//annotation&#39;s current view using the viewForAnnotation method...
MKAnnotationView *av = ;
av.draggable = editMode;
</code></pre>

<p><br/>
您还必须更新 <code>viewForAnnotation</code> <em>delegate</em> 方法中的代码,以便它还将 <code>draggable</code> 设置为 <code>editMode</code> 而不是硬编码 <code>YES</code> 或 <code>NO</code> 以便如果 mapView 需要为注释重新创建 View<em>after</em> 您已经更新了它在 for 循环中,注释 View 将具有 <code>draggable</code> 的正确值。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 启用和禁用注释拖动(动态)(iOS Mapkit),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/9995197/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/9995197/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 启用和禁用注释拖动(动态)(iOS Mapkit)