gpt4 book ai didi

swift - 如何获取 Pin 图来更改其图像?

转载 作者:行者123 更新时间:2023-11-30 13:34:01 24 4
gpt4 key购买 nike

我有问题,我的注释仍然是一个图钉!我想要按钮和图片,以显示有关用户的信息并且它有效。但它不会改变 map 上图钉的外观!这是我的注释 View :

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// 1
let identifier = "pinAnnotation"

// 2
if annotation.isKindOfClass(pinAnnotation.self) {
// 3
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

if annotationView == nil {


//4
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true


// 5
let btn = UIButton(type: .InfoDark)
annotationView!.rightCalloutAccessoryView = btn



resultImgArray[0].getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in

if error == nil {

let imageView = UIImageView(frame:CGRectMake(0,0,45,45))
imageView.image = UIImage(data: imageData!)
annotationView!.leftCalloutAccessoryView = imageView

}


}


} else {
// 6
annotationView!.annotation = annotation
}

return annotationView
}

return nil

}

这是我的 pinAnnotationView.swift 作为 MKAnnotationView:

import UIKit
import MapKit
import CoreLocation

class pinAnnotationView: MKAnnotationView {

override init(frame :CGRect) {
super.init(frame: frame)
}

override init(annotation: MKAnnotation?, reuseIdentifier: String?) {

super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)

var frame = self.frame
frame.size = CGSizeMake(80, 80)
self.frame = frame
self.backgroundColor = UIColor.clearColor()
self.centerOffset = CGPointMake(-5, -5)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code

UIImage(named: "carlogo.png")?.drawInRect(CGRectMake(30, 30, 30, 30))

}
}

这是图片
enter image description here

最佳答案

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isMember(of: MKUserLocation.self) {
return nil
}
let identifier = "someID"
var dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if dequeuedAnnotationView == nil{
dequeuedAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
configureDetailView(annotationView: dequeuedAnnotationView!)

}
return dequeuedAnnotationView
}

func configureDetailView(annotationView: MKAnnotationView) {
var frame = self.frame
frame.size = CGSizeMake(80, 80)
self.frame = frame
self.backgroundColor = UIColor.clearColor()
self.centerOffset = CGPointMake(-5, -5)
annotationView.image = UIImage(named:"yourImage")
}

并使用annotationView.detailCalloutAccessoryView = "yourView"在这种情况下将是框架。

希望对你有用

关于swift - 如何获取 Pin 图来更改其图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36285208/

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