2015-11-05 78 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