MKPointAnnotations在swift中触摸事件

前端之家收集整理的这篇文章主要介绍了MKPointAnnotations在swift中触摸事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道是否有人能告诉我如何以MKPointAnnotations的形式触摸地图上的图钉.

我想点击地图上的图钉并通过返回我预设的图钉的变量转到另一个视图.

任何人都可以在Swift中解释我这件事吗?

谢谢

使用代码编辑:

  1. class ViewController: UIViewController,MKMapViewDelegate {
  2.  
  3.  
  4. @IBOutlet weak var mappa: MKMapView!
  5.  
  6.  
  7. override func viewDidLoad() {
  8. super.viewDidLoad()
  9.  
  10. var location : CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 44.648590,longitude: 10.918794)
  11.  
  12. let pinAnnotation = PinAnnotation()
  13. pinAnnotation.setCoordinate(location)
  14.  
  15. self.mappa.addAnnotation(pinAnnotation)
  16.  
  17. }
  18.  
  19. class PinAnnotation : NSObject,MKAnnotation {
  20. private var coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0,longitude: 0)
  21.  
  22. var coordinate: CLLocationCoordinate2D {
  23. get {
  24. return coord
  25. }
  26. }
  27.  
  28. var title: String = "test"
  29. var subtitle: String = "test"
  30.  
  31. func setCoordinate(newCoordinate: CLLocationCoordinate2D) {
  32. self.coord = newCoordinate
  33. }
  34. }
  35.  
  36. func mapView(mapView: MKMapView!,viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
  37. if annotation is PinAnnotation {
  38. let pinAnnotationView = MKPinAnnotationView(annotation: annotation,reuseIdentifier: "myPin")
  39.  
  40. pinAnnotationView.pinColor = .Purple
  41. pinAnnotationView.draggable = true
  42. pinAnnotationView.canShowCallout = true
  43. pinAnnotationView.animatesDrop = true
  44.  
  45. let deleteButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
  46. deleteButton.frame.size.width = 44
  47. deleteButton.frame.size.height = 44
  48. deleteButton.backgroundColor = UIColor.redColor()
  49. deleteButton.setImage(UIImage(named: "trash"),forState: .Normal)
  50.  
  51. pinAnnotationView.leftCalloutAccessoryView = deleteButton
  52.  
  53.  
  54. return pinAnnotationView
  55. }
  56.  
  57. return nil
  58. }
  59.  
  60. func mapView(mapView: MKMapView!,annotationView view: MKAnnotationView!,calloutAccessoryControlTapped control: UIControl!) {
  61. if let annotation = view.annotation as? PinAnnotation {
  62. self.mapView.removeAnnotation(annotation)
  63. }
  64. }
  65. }
有几个步骤是必要的,这里有一些代码片段可以帮助您入门.

首先,您需要一个自定义类用于您的引脚注释,它包含您要使用的数据.

  1. import MapKit
  2. import Foundation
  3. import UIKit
  4.  
  5. class PinAnnotation : NSObject,longitude: 0)
  6.  
  7. var coordinate: CLLocationCoordinate2D {
  8. get {
  9. return coord
  10. }
  11. }
  12.  
  13. var title: String = ""
  14. var subtitle: String = ""
  15.  
  16. func setCoordinate(newCoordinate: CLLocationCoordinate2D) {
  17. self.coord = newCoordinate
  18. }
  19. }

然后你需要一个符合MKMapViewDelegate协议的MKMapView自定义类.在那里实现方法viewForAnnotation:

  1. import MapKit
  2. import CLLocation
  3. import Foundation
  4. import UIKit
  5.  
  6. class MapViewController: UIViewController,MKMapViewDelegate {
  7.  
  8. ...
  9.  
  10. func mapView(mapView: MKMapView!,viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
  11. if annotation is PinAnnotation {
  12. let pinAnnotationView = MKPinAnnotationView(annotation: annotation,reuseIdentifier: "myPin")
  13.  
  14. pinAnnotationView.pinColor = .Purple
  15. pinAnnotationView.draggable = true
  16. pinAnnotationView.canShowCallout = true
  17. pinAnnotationView.animatesDrop = true
  18.  
  19. let deleteButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
  20. deleteButton.frame.size.width = 44
  21. deleteButton.frame.size.height = 44
  22. deleteButton.backgroundColor = UIColor.redColor()
  23. deleteButton.setImage(UIImage(named: "trash"),forState: .Normal)
  24.  
  25. pinAnnotationView.leftCalloutAccessoryView = deleteButton
  26.  
  27. return pinAnnotationView
  28. }
  29.  
  30. return nil
  31. }
  32.  
  33. func mapView(mapView: MKMapView!,calloutAccessoryControlTapped control: UIControl!) {
  34. if let annotation = view.annotation as? PinAnnotation {
  35. mapView.removeAnnotation(annotation)
  36. }
  37. }

这给你这样的东西:

要在地图中添加新注释,请在代码中的某处使用:

  1. let pinAnnotation = PinAnnotation()
  2. pinAnnotation.setCoordinate(location)
  3.  
  4. mapView.addAnnotation(pinAnnotation)

猜你在找的Swift相关文章