2016-08-03 34 views
0

Ich verstehe nicht, wie ich meine benutzerdefinierten Kartenanmerkungen tapable machen kann.Benutzerdefinierte Anmerkungen anpassbar machen

Dies ist der Code, der die Anmerkungen erzeugt:

import UIKit 
import MapKit 
import CoreLocation 

class NeighbourhoodMapCell: UITableViewCell { 

    var userList : NSMutableArray? 
    var itemList : NSMutableArray? 

    @IBOutlet weak var mapView: MKMapView! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
    } 

    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 
    } 

    func loadAnnotations(){ 
     let allAnnotations = mapView.annotations 
     mapView.removeAnnotations(allAnnotations) 

     if (userList != nil){ 
      //load users 
      if let userList = self.userList{ 
       for user in userList{ 
        if let user = user as? User, let lat = user.latitude, let long = user.longitude{ 
         let location = CLLocationCoordinate2D(latitude: lat, longitude: long) 
         let annotation = UserAnnotation(coordinate: location, user: user) 
         mapView.addAnnotation(annotation) 
        } 
       } 
      } 

     } else { 
      //load items 
      if let itemList = self.itemList{ 
       for item in itemList{ 
        if let item = item as? Item, let lat = item.latitude, let long = item.longitude{ 
         let location = CLLocationCoordinate2D(latitude: lat, longitude: long) 
         let annotation = ItemAnnotation(coordinate: location, item: item) 
         mapView.addAnnotation(annotation) 
        } 
       } 
      } 
     } 
    } 
} 

extension NeighbourhoodMapCell: MKMapViewDelegate { 
    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, 
       calloutAccessoryControlTapped control: UIControl) { 
     print("1") 
    } 

    func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { 
     print("2") 
    } 

    func calloutTapped(sender:UITapGestureRecognizer) { 
     print("3") 
    } 

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
     guard !annotation.isKindOfClass(MKUserLocation) else { 
      return nil 
     } 

     let annotationIdentifier = "AnnotationIdentifier" 
     var annotationView: MKAnnotationView? 

     if (annotation.isKindOfClass(UserAnnotation) == true){ 
      let annotation = annotation as? UserAnnotation 

      if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) { 
       annotationView = dequeuedAnnotationView 
       annotationView?.annotation = annotation 
      } 
      else { 
       let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 
       av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) 
       annotationView = av 
      } 

      if let annotationView = annotationView { 
       annotationView.canShowCallout = true 
       annotationView.image = UIImage(named: "ic_annotation") 

       let rect = CGRect(x: 5, y: 5, width: 36.0, height: 36.0) 
       let annotationImageView = GFItemImageView(frame: rect) 
       annotationImageView.image = nil 
       annotationImageView.roundedImage = true 
       annotationImageView.imageSize = .Small_100x100 


       if let user = annotation!.user, imageURLString = user.imageURLString, imageURL = NSURL(string: imageURLString) { 
        annotationImageView.setImageWithUrl(imageURL, placeHolderImage: UIImage(named:"ic_profile_pic")) 
       } else { 
        annotationImageView.image = UIImage(named:"ic_profile_pic") 
       } 
       annotationView.addSubview(annotationImageView) 
      } 
     } 

     if (annotation.isKindOfClass(ItemAnnotation) == true){ 

      let annotation = annotation as? ItemAnnotation 

      if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) { 
       annotationView = dequeuedAnnotationView 
       annotationView?.annotation = annotation 
      } 
      else { 
       let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 
       av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) 
       annotationView = av 
      } 

      if let annotationView = annotationView { 
       annotationView.canShowCallout = true 
       annotationView.image = UIImage(named: "ic_annotation") 

       let rect = CGRect(x: 5, y: 5, width: 36.0, height: 36.0) 
       let annotationImageView = GFItemImageView(frame: rect) 
       annotationImageView.image = nil 
       annotationImageView.roundedImage = true 
       annotationImageView.imageSize = .Small_100x100 

       if let item = annotation?.item, let itemImageURLString = item.imageURLString, let itemImageURL = NSURL(string: itemImageURLString){ 
        annotationImageView.setImageWithUrl(itemImageURL, placeHolderImage: nil) 
       } else { 
        annotationImageView.image = UIImage(named: "ic_item_pic_\(NSLocale.preferredLanguages()[0].containsString("zh") ? "zh" : "en")") 
       } 

       annotationView.addSubview(annotationImageView) 
      } 
     } 
     return annotationView 
    } 
} 

class UserAnnotation : NSObject, MKAnnotation { 
    var user:User? 
    var coordinate: CLLocationCoordinate2D 
    var title: String? 

    init(coordinate: CLLocationCoordinate2D, user: User?) { 
     self.coordinate = coordinate 
     self.user = user 
    } 
} 

class ItemAnnotation : NSObject, MKAnnotation { 
    var item:Item? 
    var coordinate: CLLocationCoordinate2D 
    var title: String? 

    init(coordinate: CLLocationCoordinate2D, item:Item?) { 
     self.coordinate = coordinate 
     self.item = item 
    } 
} 

Das in den folgenden Modus führt:

enter image description here

Jedoch, wenn ich auf die Anmerkung oder das Bild klicken Ich habe oben hinzugefügt nichts passiert. Ich hatte gehofft, es in eine der Methoden Druck 1, 2 oder 3.

Antwort

0

behoben, indem schießen würde:

annotationView.canShowCallout = true 

An: wird nun

annotationView.canShowCallout = false 

Tapping eine Anmerkung auslösen:

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { 
    print("2") 
}