【发布时间】:2014-09-03 08:27:12
【问题描述】:
我试图找到一种方法在 Swift MapFramework 中添加指向注释的链接,此链接应将用户转发到 WebView,据我所知,我找不到任何方法将“可触摸”链接添加到注释副标题
这是我的代码
class CustomPointAnnotation: MKPointAnnotation {
var imageName: String!
}
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"
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
}
有没有办法为此使用 UIGestureRecognizer?
我已经试过了
var longpress = UILongPressGestureRecognizer(target: self, action: "newInformation:")
longpress.minimumPressDuration = 2.0
info1.addGestureRecognizer(longpress)
但以“ViewController.CustomPointAnnotation 没有名为 addGestureRecognizer 的成员”结尾
【问题讨论】:
-
您提出的许多问题已经在documentation 或 SO 上得到解答。在文档中,搜索 MKMapView 类参考或阅读Location and Maps Programming Guide。目前关于 SO 的答案主要在 Objective-C 中,但在 Swift 中的工作完全相同相同(只需要翻译语言)。
-
对于当前这个问题,典型的做法是通过设置视图的leftCalloutAccessoryView或者rightCalloutAccessoryView来添加一个callout附件按钮。有关示例,请参阅stackoverflow.com/questions/25202613/mapkit-in-swift-part-2。点击按钮时,地图视图将调用 calloutAccessoryControlTapped 委托方法。在那里,您可以展示网络视图。
-
感谢 Anna 的帮助,对于这个问题,我很抱歉,我刚开始学习 swift 和 objectiv c,有时即使我看到了答案,我也不明白 ;)!
-
好的,没问题。尝试在链接问题中进行更改。如果你得到它的工作,你可以自己回答这个问题。否则,请使用新代码和问题更新此问题。
-
小添加的问题 Anna(或者我应该为此开始一个新主题)有没有办法查看选择了什么“注释”?我尝试使用 self.title 来获取 info1/info2 的标题
标签: map webview hyperlink swift annotations