菜鸟教程小白 发表于 2022-12-11 19:29:36

ios - AnnotationView 在 calloutVIew 上重叠


                                            <p><p>我正在使用 MapKit 在 map 上显示数据。为此,我使用自定义 AnnotationView 和自定义 CalloutView。但问题是,当我让 AnnotationView 相互关闭时,AnnotationView 在 CalloutView 上重叠。这是问题的截图。<br/><br/>
<a href="/image/n9q59.jpg" rel="noreferrer noopener nofollow"><img src="/image/n9q59.jpg" alt="overlapping annotationView"/></a> </p>

<p>按钮也有问题,按钮的点击事件没有被调用。 calloutView 下方的 4 个按钮不会在点击时被调用。但是右上角代表编辑事件的按钮在点击时会被触发。</p>

<p>这是我的 CalloutView 代码。</p>

<pre><code>@implementation CustomCalloutView

- (id)init {

      return self;
}
- (IBAction)btnEditAction:(UIButton *)sender {

    ;
}

- (IBAction)btnMailAction:(UIButton *)sender {

    ;
}

- (IBAction)btnMessageAction:(UIButton *)sender {

    ;
}

- (IBAction)btnCallAction:(UIButton *)sender {

    ;

}

- (IBAction)btnStreetAction:(UIButton *)sender {

    ;
}
- (IBAction)CalloutTapGestureClicked:(UITapGestureRecognizer*)sender {
    ;
}
@end
</code></pre>

<p>请帮我解决这个问题...</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这样做可以防止重叠(在顶部插入 callOutView )</p>

<pre><code> func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)
{

   print(&#34;clickedddddddd&#34;)

    if view.annotation is MKUserLocation
    {
      return
    }

      let customView = (Bundle.main.loadNibNamed(&#34;customSou&#34;, owner: self, options: nil))? as! customSouOut;

      var calloutViewFrame = customView.frame;
      calloutViewFrame.origin = CGPoint(x:-calloutViewFrame.size.width/2 + 15,y: -calloutViewFrame.size.height);
      customView.frame = calloutViewFrame;


      view.addSubview(customView)


         // here you can fill any label or button with data according to it&#39;s pin

         //

      for v in view.subviews
      {
            if v is customSouOut
            {
                continue
            }
            else
            {
                view.insertSubview(customView, aboveSubview: v)
            }
      }

    }

}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView)
{
   print(&#34;oppspspspsps&#34;)

    if view.annotation is MKUserLocation
    {
      return
    }

   for v in view.subviews
    {
      if v is customSouOut
      {
            v.removeFromSuperview()

            break
      }
    }




}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - AnnotationView 在 calloutVIew 上重叠,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47912093/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47912093/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - AnnotationView 在 calloutVIew 上重叠