ios 用于注释的 Swift 不同图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25631410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift different images for Annotation
提问by Fabian Boulegue
I managed to get a custom icon for a annotation pin in Swift, but now I am still stuck using 2 different images for different annotations. Right now a button adds a annotation to the map. There should be another button that also adds a annotation but with another icon.
我设法在 Swift 中获得了一个注释图钉的自定义图标,但现在我仍然坚持使用 2 个不同的图像进行不同的注释。现在,一个按钮会向地图添加注释。应该有另一个按钮也添加了注释,但带有另一个图标。
Is there a way to use the reuseId for this?
有没有办法为此使用重用 ID?
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var Map: MKMapView!
@IBAction func btpressed(sender: AnyObject) {
var lat:CLLocationDegrees = 40.748708
var long:CLLocationDegrees = -73.985643
var latDelta:CLLocationDegrees = 0.01
var longDelta:CLLocationDegrees = 0.01
var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long)
var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
Map.setRegion(region, animated: true)
var information = MKPointAnnotation()
information.coordinate = location
information.title = "Test Title!"
information.subtitle = "Subtitle"
Map.addAnnotation(information)
}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if !(annotation is MKPointAnnotation) {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.image = UIImage(named:"1.png")
anView.canShowCallout = true
}
else {
anView.annotation = annotation
}
return anView
}
回答by
In the viewForAnnotation
delegate method, set the image
based on which annotation
the method is being called for.
在viewForAnnotation
委托方法中,设置调用方法的image
基础annotation
。
Be sure to do this afterthe view is dequeued or created (and not only in the if anView == nil
part). Otherwise, annotations that use a dequeued view will show the image of the annotation that used the view previously.
一定要做到这一点后视图被出队或创建的(而不是只在if anView == nil
部分)。否则,使用出列视图的注释将显示之前使用该视图的注释的图像。
With the basic MKPointAnnotation
, one crude way to tell annotations apart is by their title
but that's not very flexible.
使用 basic MKPointAnnotation
,区分注释的一种粗略方法是通过它们,title
但这不是很灵活。
A better approach is to use a custom annotation class that implements the MKAnnotation
protocol (an easy way to do that is to subclass MKPointAnnotation
) and add whatever properties are needed to help implement the custom logic.
更好的方法是使用实现MKAnnotation
协议的自定义注释类(一种简单的方法是子类MKPointAnnotation
)并添加任何需要的属性来帮助实现自定义逻辑。
In the custom class, add a property, say imageName
, which you can use to customize the image based on the annotation.
在自定义类中,添加一个属性,例如imageName
,您可以使用该属性根据注释自定义图像。
This example subclasses MKPointAnnotation
:
此示例子类MKPointAnnotation
:
class CustomPointAnnotation: MKPointAnnotation {
var imageName: String!
}
Create annotations of type CustomPointAnnotation
and set their imageName
:
创建类型的注释CustomPointAnnotation
并设置它们imageName
:
var info1 = CustomPointAnnotation()
info1.coordinate = CLLocationCoordinate2DMake(42, -84)
info1.title = "Info1"
info1.subtitle = "Subtitle"
info1.imageName = "1.png"
var info2 = CustomPointAnnotation()
info2.coordinate = CLLocationCoordinate2DMake(32, -95)
info2.title = "Info2"
info2.subtitle = "Subtitle"
info2.imageName = "2.png"
In viewForAnnotation
, use the imageName
property to set the view's image
:
在 中viewForAnnotation
,使用imageName
属性来设置视图的image
:
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if !(annotation is CustomPointAnnotation) {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.canShowCallout = true
}
else {
anView.annotation = annotation
}
//Set annotation-specific properties **AFTER**
//the view is dequeued or created...
let cpa = annotation as CustomPointAnnotation
anView.image = UIImage(named:cpa.imageName)
return anView
}
回答by Vinod Joshi
iOS Swift Code With Help of Anna and Fabian Boulegue:
在 Anna 和 Fabian Boulgue 的帮助下的 iOS Swift 代码:
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.mapView.delegate = self
var info1 = CustomPointAnnotation()
info1.coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042)
info1.title = "Info1"
info1.subtitle = "Subtitle"
info1.imageName = "flag.png"
var info2 = CustomPointAnnotation()
info2.coordinate = CLLocationCoordinate2DMake(26.862280, 75.815098)
info2.title = "Info2"
info2.subtitle = "Subtitle"
info2.imageName = "flag.png"
mapView.addAnnotation(info1)
mapView.addAnnotation(info2)
}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
println("delegate called")
if !(annotation is CustomPointAnnotation) {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.canShowCallout = true
}
else {
anView.annotation = annotation
}
//Set annotation-specific properties **AFTER**
//the view is dequeued or created...
let cpa = annotation as CustomPointAnnotation
anView.image = UIImage(named:cpa.imageName)
return anView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
class CustomPointAnnotation: MKPointAnnotation {
var imageName: String!
}