我正在开发一个使用 swift 3.0 和 Mapkit 的项目。
现在我想触摸注释
func mapView(_ mapView: MKMapView,
didSelect view: MKAnnotationView)
{
print("Annotation touched!");
}
但是当我触摸注释时, map 会向左移动。
希望你能帮助我!
谢谢!
最佳答案
您需要在注释中放置一个按钮:
let smallSquare = CGSize(width: 30, height: 30) // The size of the button.
let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
button.addTarget(self, action: #selector(ViewController.a_function_where_you_can_put_anything), forControlEvents: .TouchUpInside)
your_pin?.leftCalloutAccessoryView = button
注意! 您应该在初始化注释时放置此代码。
例如:
let reuseId = "pin"
var your_pin = your_map.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
// Your pin
let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
button.addTarget(self, action: #selector(ViewController.a_function_where_you_can_put_anything), forControlEvents: .TouchUpInside)
your_pin?.leftCalloutAccessoryView = button
像这样,在注释中初始化一个按钮,当用户选择它时,函数 a_function_where_you_can_put_anything () (在我的示例中)正在调用。我给你一个这个函数的例子:
func a_function_where_you_can_put_anything () { // This function is calling when the button was selected.
print ("Annotation touched!");
}
但是,该按钮仅适用于注释的左侧。您可以使用一张图片来向用户的按钮发出信号:your_pin.setBackgroundImage(UIImage(named: "your_image"), forState: .Normal)
。
您可以在 apple developer web site 中了解更多详细信息.
这段代码不如你的漂亮,但它可以工作。
诚挚的。
关于Swift 3.0 mapkit didSelect 将 MKMapView 移至左侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41742053/