Swift 3.0 mapkit didSelect 将 MKMapView 移至左侧

标签 swift swift3 mkmapview mapkit mkannotation

我正在开发一个使用 swift 3.0 和 Mapkit 的项目。

现在我想触摸注释

 func mapView(_ mapView: MKMapView,
             didSelect view: MKAnnotationView)
{

    print("Annotation touched!");


}

但是当我触摸注释时, map 会向左移动。

image from the 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/

相关文章:

image - 将UIimage(拍照)转成NSData

ios - 如何在 iOS 中的 MKMapview 上隐藏圆形罗盘图标

ios - 如何从 CAKeyframeAnimation 获取当前动画步骤

swift - Xcode Source Kit 不会停止与 Swift 崩溃

ios - 来自外部源的 iframe 内容未加载到 iOS 上的本地 HTML 文件中

ios - 如何比较 2 个数组并将最后一项附加到第二个数组 iOS Swift 3

swift - 在 Viewcontrollers 中更新导航 - Coredats

swift - 从变量分配 UIButton 目标

ios - 寻找 MKOverlayPathRenderer 示例

objective-c - MKMapView 中的注释未显示标题