gpt4 book ai didi

ios - Swift - MKPinAnnotation 图像问题

转载 作者:搜寻专家 更新时间:2023-10-31 19:31:29 28 4
gpt4 key购买 nike

我正在尝试将我的 pin 注释图像更改为 pin 以外的其他内容。

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}

let reuseId = "pin"

var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.pinColor = .Green
pinView!.image = UIImage(named: "icon1.png")

// Add image to left callout
var mugIconView = UIImageView(image: UIImage(named: "test.png"))
pinView!.leftCalloutAccessoryView = mugIconView

// Add detail button to right callout
var calloutButton = UIButton.buttonWithType(.DetailDisclosure) as UIButton
pinView!.rightCalloutAccessoryView = calloutButton
}
else {
pinView!.annotation = annotation
}

return pinView

除了更改图钉图像外,一切正常。我有一个自定义点注释子类,它是这个

我已经搜索了几个小时,但我发现的所有其他 SO 问题都没有帮助。如果有任何帮助,我将不胜感激……谢谢。

更新了答案

   func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}

var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier("pin")

if pinView == nil {
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
pinView!.canShowCallout = true
pinView!.image = UIImage(named: "test.png")

// Add image to left callout
var mugIconView = UIImageView(image: UIImage(named: "test.png"))
pinView!.leftCalloutAccessoryView = mugIconView

// Add detail button to right callout
var calloutButton = UIButton.buttonWithType(.DetailDisclosure) as UIButton
pinView!.rightCalloutAccessoryView = calloutButton
}
else {
pinView!.annotation = annotation
}

return pinView
}

最佳答案

您需要 MKAnnotationView,而不是 MKPinAnnotationView。 Pin 注释无法自定义其图像。

参见 this SO answer for additional details

关于ios - Swift - MKPinAnnotation 图像问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27369265/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com