我正在将 map 注释集成到我的应用程序中。我在 map 上添加注释如下。 MAP上的所有标注都添加成功。
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if (annotation is MKUserLocation) {
return nil
}
let reuseId = "CustomAnnotation"
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
annotationView.image = UIImage(named:"MapTemp")
annotationView.canShowCallout = false
return annotationView
}
我在点击 map 注释时显示一个 customView。单击注释时,将调用其委托(delegate),其中将自定义 View 添加到 map 上,如下面的代码所示-
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
{
if (self.viewCustom != nil)
{
self.viewCustom.removeFromSuperview()
}
let nib:Array = NSBundle.mainBundle().loadNibNamed("View123", owner: self, options: nil)
self.viewCustom = nib[0] as? UIView
println(self.viewCustom.viewWithTag(100))
if let buttonFB = self.viewCustom.viewWithTag(100) as? UIButton {
buttonFB.addTarget(self, action:"clickFacebook:", forControlEvents: UIControlEvents.TouchUpInside)
}
self.addBounceAnimationToView(viewCustom)
var hitPoint : CGPoint = view.convertPoint(CGPointZero, toView: viewCustom)
self.viewCustom!.frame = CGRectMake(-100,-200, 250, 200)
view.addSubview(self.viewCustom!)
self.view.bringSubviewToFront(self.viewCustom)
}
在自定义 View 中,我有一个按钮 buttonFB
指定了标签值和事件 clickFacebook
。
@IBAction func clickFacebook(sender: AnyObject)
{
//Face
}
但是这个按钮的点击事件不会触发,只有 Mapview 的委托(delegate) didDeselectAnnotationView
总是被调用(下面的方法被调用)。
func mapView(mapView: MKMapView!, didDeselectAnnotationView view: MKAnnotationView!)
{
//
}
我想让按钮的事件触发。我该怎么做。
darkgray Image是MapView的Annotation,黄色部分是根据图片的button。
请帮帮我。
最佳答案
在 didSelectAnnotationView:
方法中,您将 self.view
放在前面,因此 self.view
将位于包含 map 的顶部,因此这些事件将在 self view
上运行。您的 self.viewCustom
位于 self.view
下方,当您触摸 self.view
时会处理触摸。
您需要将您的 self.viewCustom
置于 self.view
的顶部,然后它才能进行触摸。
你需要改变声明-
self.viewCustom.bringSubviewToFront(self.view)
到
self.view.bringSubviewToFront(self.viewCustom)
关于ios - 按钮事件不会在 MAP 上的 CustomView 内部调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30435134/