2015-11-05 80 views
1

如果在mapView didChangeDragState中檢測到註釋已被拖動到不需要的位置,我可以取消拖動mapView:didChangeDragState。如何撤消MapKit註釋拖動

case .Dragging: 
      let inColorado = StateOutline.inColorado(view.annotation!.coordinate) 
      if !inColorado { 
       view.dragState = .Canceling 
      } 

不幸的是,這會將銷釘留在取消拖動的不需要位置的位置。

一想設置註釋

  • 有效座標
  • 或恢復到前拖拽座標
  • 或設置拖動的最後一個有效位置

    case .Canceling: 
         view.annotation!.coordinate = StateOutline.coloradoCenter() 
         view.dragState = .None 
        } 
    

該座標設置是不允許的,因爲view.ann otation!.coordinate是一個只讀屬性。

如何撤銷註釋拖動?

使用MKAnnotation setCoordinate不是可以考慮的事情 - 它在iOS 8.3中被刪除。

唯一想到的就是用一個新註釋替換該註釋並設置座標。理想情況下,銷座標將被設置爲其最後的有效位置。

回答

0

這裏你的錯誤是認爲註釋的座標不能設置。它可以。 MKAnnotation協議並不規定可設置的座標,但所有實際使用者都有一個。只需使用MKPointAnnotation即可。它的coordinate是可設置的。你甚至可能已經在使用一個!

public class MKPointAnnotation : MKShape { 
    public var coordinate: CLLocationCoordinate2D 
} 

你甚至可以編寫自己的MKAnnotation採納者:

import UIKit 
import MapKit 

class MyAnnotation : NSObject, MKAnnotation { 
    dynamic var coordinate : CLLocationCoordinate2D 
    var title: String? 
    var subtitle: String? 

    init(location coord:CLLocationCoordinate2D) { 
     self.coordinate = coord 
     super.init() 
    } 
} 
+0

感謝您的幫助。已經擁有了我自己的註釋類,而不是MKPointAnnotation。什麼工作,把邏輯放在.Ending案例中: 讓annotation = view.annotation as! MyAnnotation! 我缺少的關鍵就是用一個可設置的座標獲得註釋。 用戶仍然可以將引腳拖動到任意位置(甚至導致滾動地圖視圖,儘管zoomEnabled和scrollEnabled設置爲false)。 – Refactor