自定义标注抓住触摸事件

时间:2015-05-20 09:27:33

标签: swift mkmapview uitouch smcalloutview calloutview

我正在使用SMCalloutView为我的mapView创建自定义标注。我遇到的问题是当标注视图可见并且用户按下标注时,如果另一个注释位于标注视图下,它将关闭当前选定的注释并选择可见标注视图下的标注。

我想也许我可以创建一个UIView子类来捕获所有的触摸事件。所以我创建了一个UIView子类,如果我将它直接添加到mapView,所有函数都会正确触发。但是,当我将它用于弹出窗口时,它似乎根本不起作用。

 import Foundation
 import UIKit

class popOverSub: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)

    let myTap = UITapGestureRecognizer(target: self, action: "touchEventTest")
    addGestureRecognizer(myTap)
    userInteractionEnabled = true
    backgroundColor = UIColor.greenColor()

}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func touchEventTest() {

    println("touchEventTest")
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    println("touch begins")
}
}

为了完成,我在这里调用自定义CalloutView。

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {

 let popView = popOverSub(frame: CGRect(x: 0, y: 0, width: 100, height: 200))
            calloutView.contentView = popView
            self.calloutView.calloutOffset = CGPoint(x: view.frame.width*0.5, y: 0)
            self.calloutView.delegate = self

            calloutView.presentCalloutFromRect(calloutView.bounds, inLayer: view.layer, constrainedToLayer: self.mapView.layer, animated: true)
}

我需要做什么。如果用户按下了标注视图,它不会关闭标注,也不会将触摸事件传递给下面的mapView / Annotation。相反,它需要调用一个函数。任何提示都会很棒。

0 个答案:

没有答案
相关问题