2017-09-01 3 views
0

Ma sous-classe personnalisée MKAnnotationView ne définit pas sa propriété image. Au lieu de cela, il gère certaines sous-vues à afficher.La zone tactile MKAnnotationView personnalisée est ridiculement étroite

Je implémente public func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) dans mon délégué pour détecter le contact d'annotation. Étant donné que la zone tactile est calculée par défaut avec la propriété image, ma zone tactile de vue d'annotation est environ 1px grande. C'est ridicule.

Voilà comment je le crée:

init(annotation: JZStreamAnnotation) { 
    self.imageView = UIImageView.init(image: <an image>) 
    self.imageView.contentMode = .center 
    super.init(annotation: annotation, reuseIdentifier: JZStreamAnnotationView.identifier) 
    self.canShowCallout = false 
    self.imageView.center = CGPoint(x: self.frame.width/2, y: self.frame.height/2) 
    self.addSubview(self.imageView) 
} 

I read this post, mais je ne peux pas comprendre comment fonctionne la méthode hitTest. Voici une mise en œuvre qui font rien:

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { 
    if point.x > -100, point.x < 100, point.y > -100, point.y < 100 { 
     //return self 
     //return self.imageView 
     return super.hitTest(point, with: event) 
    } 
    return .none 
} 

A breapoint situé dans les if feux de clause, mais le retour tout (super/self/imageView) ne fait rien.

Enfin, si je mets en œuvre hitTest comme ceci:

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { 
    return .none 
} 

et si je clic sur mon annotationView centre exact, la méthode déléguée public func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) si tiré!

Un conseil pour agrandir la zone tactile?

Répondre

0

Pour autant que je compris, func point(inside:with:) et hitTest(point:with:) ne contribuent pas à agrandir la zone qui appellent le délégué public func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)

Enfin, j'ai fini en élargissant le cadre MKAnnotationView pour enfermer mes sous-vues d'annotation et de déplacer le point d'ancrage exactement où je voulais que mon centre d'annotation

self.frame = CGRect(x: 0, y: 0, width: 
self.myLabel.frame.width, height: self.myLabel.frame.height) 
self.centerOffset = CGPoint(x: self.frame.width/2, y: self.myImageView.frame.height/2) 

Je supprimé point(inside:with:) et hitTest(point:with:) qui l'emporte n'a rien fait. C'est la seule façon dont j'ai réussi à rendre ma vue d'annotation totalement réactive.