2014-12-05 183 views
0

enter image description here自定义MKAnnotationView与多个图像

上面是我想要做的事情的一个例子。我有两个图像,“marker.png”和“userImage.png”。我可以让每个单独显示为注释,但是我想将标记背景上的userImage覆盖为一个注释。看起来上面的图片用白色标记和顶部的图片做了这样的事情。我看到MKAnnotationView将UIImage作为一个属性,但没有UIImageView。我怎样才能完成我想要做的事情?

目前我能够只显示一个图像

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
    if !(annotation is MKPointAnnotation) { 
     return nil 
    } 

    let reuseId = "CustomMarker" 

    var markerView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) 
    if markerView == nil { 
     markerView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     markerView.image = UIImage(named: "marker.png") 
    } 
    else { 
     markerView!.annotation = annotation 
    } 

    return markerView 
} 

回答

2

实现自己MKAnnotationView子类。现在你已经有了一个完全属于你的视图!它可以有任何你想要的子视图,或者(这是我会做的),你可以实现drawRect:并绘制它;在drawRect:中绘制复合图像或排列图像很容易。

或者,你可以继续做你正在做的事情,但在代码中合成图像,并使用生成的组合图像作为image属性。这也很简单。

+0

事实上,我很难理解你认为的难点在这里!在代码中绘制/组合图像与iOS编程一样基本。如果我误解了这个问题,请告诉我。 – matt 2014-12-05 00:58:13

+0

我对开发相对比较陌生,所以这帮忙让我看看正确的东西。谢谢!懂得它的作用 – 2014-12-05 22:39:25

+0

是的,我的书的第二章(就在“什么是视图”一章之后)是关于_drawing_的原因。这是真正了解如何在iOS编程中做的最基本和最重要的事情之一! – matt 2014-12-06 03:15:17