gpt4 book ai didi

arrays - 在 leftcalloutaccessoryview 中使用 for loop array 在 swift 注释映射中设置唯一图像

转载 作者:行者123 更新时间:2023-11-28 09:12:12 25 4
gpt4 key购买 nike

我有一组图像需要在 MKAnnotation 的 leftcalloutaccessoryview 中设置为左图标。我已经使用 if else 语句完成了它。我的问题是,如果我可以使用数组循环在“for”语句中编写代码,而不是使用“if else”语句,那就太好了。有人可以帮我解决这个问题吗?如何使用 for 循环数组使用一组图像和数组中的坐标对“leftcalloutaccessoryview”进行编码?我已经面对这段代码一个月了:(...谢谢你们

    //Define nearby Place of Interest
let latArray = [24.469546, 24.469450]
let longArray = [39.609105, 39.613062]
let poiTitle = ["An-Nisa 1","An-Nisa 2"]
let poiSubTitle = ["Gate 1","Gate 2"]
// let imageName = ["Jons.png","Locus.png"]

for var i=0;i<poiSubTitle.count;i++
{
var latPoi: CLLocationDegrees = latArray[i]
var longPoi: CLLocationDegrees = longArray[i]

var locationPoiAnnot = MKPointAnnotation()

locationPoiAnnot.title = poiTitle[i]
locationPoiAnnot.subtitle = poiSubTitle[i]
var locationPoi : CLLocationCoordinate2D = CLLocationCoordinate2DMake(latPoi,longPoi)
locationPoiAnnot.coordinate = locationPoi


self.mapView.addAnnotation(locationPoiAnnot)

}

}
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

let arrowButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
arrowButton.frame.size.width = 44
arrowButton.frame.size.height = 44
arrowButton.setImage(UIImage(named: "arrow.jpeg"), forState: .Normal)

pinView!.rightCalloutAccessoryView = arrowButton

var imageview = UIImageView(frame: CGRectMake(0, 0, 45, 45))

if(annotation.subtitle == "I am here!")
{

imageview.image=UIImage(named: "Locus.png")

}
else if(annotation.subtitle == "Gate 1")
{
imageview.image=UIImage(named: "Jons.png")

}

else if(annotation.subtitle == "Gate 2")
{

imageview.image=UIImage(named: "Katara.png")

}


pinView!.leftCalloutAccessoryView = imageview


}
else {
pinView!.annotation = annotation
}
return pinView }

最佳答案

想知道这是否有帮助:

let poiSubTitle = ["Gate 1","Gate 2"]
let imageName = ["Jons.png","Locus.png"]

for(var i = 0 ; i < imageName.count ; i++)
{
if(annotation.subtitle == poiSubTitle[i])
{
imageview.image = UIImage(named: imageName[i])
}
}

pinView!.leftCalloutAccessoryView = imageview

因此只需将所有 30 个 POI 标题添加到 poiSubTitle 并将图像路径添加到 imageName 而不更改 for 循环...

关于arrays - 在 leftcalloutaccessoryview 中使用 for loop array 在 swift 注释映射中设置唯一图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28956737/

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