2017-09-01 88 views
0

我的自定义MKAnnotationView子类不要设置它的image属性。相反,它处理一些子视图来显示。自定义MKAnnotationView触摸区域可笑地缩小

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

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

这是我如何创建它:

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,但我想不通的则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 
} 

,如果我在我的annotationView确切中心CLIC,如果解雇了public func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)委托方法!

任何建议放大触摸区?

回答

0

据我了解,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:)将覆盖什么也没做。

这是我成功使我的注释视图完全被动的唯一方法。