自定义MKAnnotationView触摸区域是可笑的狭窄

我的自定义MKAnnotationView子类不设置它的image属性。 相反,它处理一些子视图来显示。

我在我的委托实现public func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)来检测注释触摸。

由于触摸区域默认使用image属性进行计算,所以我的注释视图触摸区域大约为1px。 这是荒唐的。

这是我创build它的方式:

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

我阅读这篇文章 ,但我无法弄清楚hitTest方法是如何工作的。 这里没有做任何事情的实现:

 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 } 

位于if子句中的breapoint会触发,但是返回( super / self / imageView )什么也不做。

最后,如果我执行这样的hitTest:

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

如果我的注释查看确切的中心, public func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)委托方法,如果开除!

任何build议放大触摸区? 🤔

据我所知, func point(inside:with:)hitTest(point:with:)不利于放大调用委托public func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)

最后,我通过放大MKAnnotationView框架来包含我的注释子视图,并将定位点移动到我想要的注释中心

 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) 

然后我删除point(inside:with:)hitTest(point:with:)覆盖,什么也没做。

这是我成功地使我的注释视图充分反应的唯一方法。