简体   繁体   English

iOS Swift MapKit为什么MKPointAnnotation是可拖动的而符合MKAnnotation的类却不是

[英]iOS Swift MapKit why MKPointAnnotation is draggable while a class that conforms MKAnnotation is not

i have a class that is a subclass of NSManagedObject that conform to MKAnnotation and then i use that in a MapView that dequeues some locations from CoreData 我有一个类,它是符合MKAnnotation的NSManagedObject的子类,然后在MapView中使用它从CoreData某些位置出CoreData

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 then add the fetched objects to the MapView as MKAnnotation like that 然后我像这样将获取的对象作为MKAnnotation添加到MapView

self.MapView.addAnnotations(self.locations)

and in the viewForAnnotation i made a subclass of MKAnnotationView that has a rounded imageView viewForAnnotation我制作了MKAnnotationView的子类,该子类具有舍入的MKAnnotationView

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)
    }

}

then I set the annotationView to be draggable in the viewForAnnotation 然后我将annotationView viewForAnnotation设置为draggableviewForAnnotation draggable

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
 }

to make the annotationView to be draggable we should implement the didChangeDragState delegate method 为了使annotationViewdraggable我们应该实现didChangeDragState委托方法

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
        }
}

if i try to drag the annotation on the map it doesn't work 如果我尝试在地图上拖动注释,则无法正常工作

* The solution that i don't like * *我不喜欢的解决方案*

The way i got it work as of the title of this question says is to use MKPointAnnotation and i mean by that is each time i add an annotation to the map i convert to MKPointAnnotation which made me make another subclass of MKPointAnnotation so that i can keep track of the location 就这个问题的标题而言,我得到它的方式是使用MKPointAnnotation ,我的意思是,每次我向地图添加注释时,我将其转换为MKPointAnnotation ,这使我成为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
    }

}

and then for adding it to the MapView 然后将其添加到MapView

for location in self.locations {
                    let pointAnnotation = AMKPointAnnotation(location: location)
                    self.MapView.addAnnotation(pointAnnotation)
}

any one tried it before ? 有人尝试过吗? What am I doing wrong? 我究竟做错了什么?

The coordinate property changes with dragging and it must be read/write and since it was a computed property with no setter the MapKit prevented the dragging for that coordinate属性随拖动而变化,并且它必须是可读写的,并且由于它是一个没有设置器的计算属性,因此MapKit阻止了拖动

* here is the solution * *这是解决方案*

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM