2016-07-29 10 views
0

ない間MKPointAnnotationがドラッグ可能である理由私はMKAnnotationに準拠NSManagedObjectのサブクラスであるクラスを持っているし、私はMapViewCoreDataiOSのスウィフトMapKit MKAnnotationに準拠クラスが

class Location: NSManagedObject , MKAnnotation { 

    var coordinate : CLLocationCoordinate2D { 
     return CLLocationCoordinate2D(latitude: Double(self.latitude), longitude: Double(self.longitude)) 
    } 

    var title: String? { 
     return self.name 
    } 

    var subtitle: String? { 
     return self.category 
    } 
} 
からいくつかの場所をデキューしていることを使用します

iがその

self.MapView.addAnnotations(self.locations) 

MKAnnotationとしてMapViewにフェッチされたオブジェクトを追加し、viewForAnnotation Iにおいてのサブクラスを作っ

class AMKAnnotationView: MKAnnotationView { 

    var imageView = UIImageView() 

    init(annotation: MKAnnotation?, reuseIdentifier: String?, size: CGSize) { 
     super.init(annotation: annotation, reuseIdentifier: reuseIdentifier) 
     self.frame = CGRectMake(0, 0, size.width, size.height) 
     self.commonInit() 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
    } 

    required init?(coder: NSCoder) { 
     super.init(coder: coder) 
    } 

    func commonInit() { 
     self.imageView.frame = CGRectMake(0, 0, self.frame.width, self.frame.height) 
     self.imageView.layer.masksToBounds = true 
     self.imageView.layer.cornerRadius = self.imageView.frame.height/2 
     self.imageView.layer.borderWidth = 5.0 
     self.imageView.layer.borderColor = UIColor.whiteColor().CGColor 
     self.imageView.userInteractionEnabled = false 
     self.addSubview(imageView) 
     self.sendSubviewToBack(self.imageView) 
    } 

} 

その後、丸みを帯びたImageViewのを持っているMKAnnotationViewは、私たちはdidChangeDragStateデリゲートメソッドを実装する必要がありdraggableをするannotationViewを作るためにviewForAnnotation

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 

     var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("pin") as? AMKAnnotationView 

     if annotationView == nil { 
      annotationView = AMKAnnotationView(annotation: annotation, reuseIdentifier: "pin", size: CGSizeMake(65, 65)) 
      annotationView?.draggable = true 
      annotationView?.canShowCallout = true 

      let index = ... // i get the index 
      let location = ... // i get the current location 

      annotationView?.imageView.image = UIImage(named: "No Photo") 

     } 

     return annotationView 
} 

draggableをするannotationViewを設定

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) { 

     switch newState { 
     case .Starting: 
      view.dragState = .Dragging 
     case .Ending, .Canceling: 
      view.dragState = .None 
      // then i save the changes to CoreData   
      } 
     default: break 
     } 
} 

マップ上に注釈をドラッグしようとした場合仕事

は*私はそれがこの質問のタイトルのように動作してしまった方法が言う

MKPointAnnotationを使用することであり、私はそれで意味*好きではない解決策は、私が追加するたびにあります私は場所

class AMKPointAnnotation : MKPointAnnotation { 

    var location : Location! 

    init(location:Location) { 
     super.init() 
     self.location = location 
     self.title = location.title 
     self.subtitle = location.subtitle 
     self.coordinate = location.coordinate 
    } 

} 

を追跡して、MapView

に追加するためにできるように、私は MKPointAnnotationの別のサブクラスを作る作られた私は MKPointAnnotationに変換マップへの注釈
for location in self.locations { 
        let pointAnnotation = AMKPointAnnotation(location: location) 
        self.MapView.addAnnotation(pointAnnotation) 
} 

これまでに試したことはありますか?私は間違って何をしていますか?ドラッグして、読み取り/書き込みする必要があり、以来、それは無いセッターMapKitと計算された財産だったと

+1

'viewForAnnotation'では' annotationView'に 'annotation'を割り当てるのを忘れてしまいました。これは受信する 'annotation'パラメータである必要があります:' annotationView.annotation = annotation' – matt

答えて

0

coordinateプロパティの変更は、その

ためにドラッグを防止*ここでの解決策は、

class Location: NSManagedObject , MKAnnotation { 

var coordinate : CLLocationCoordinate2D { 
    set { 
      self.latitude = newValue.latitude 
      self.longitude = newValue.longitude 
     } 
     get { 
      return CLLocationCoordinate2D(latitude: Double(self.latitude), longitude: Double(self.longitude)) 
     } 
    } 

    var title: String? { 
     return self.name 
    } 

    var subtitle: String? { 
     return self.category 
    } 
} 
*です
関連する問題