func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
/*
* Other stuff ....
*/
visibleCallOutView = CustomCallOut(frame: aFrame)
view.addSubview(visibleCallOutView)
}
func mapView(mapView: MKMapView!, didDeselectAnnotationView view: MKAnnotationView!) {
visibleCallOutView.removeFromSuperview()
}
下面是自定义标注类
class CustomCallOut: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
let button = UIButton(frame: bounds)
button.addTarget(self, action: "callOutButtonTouchAction:", forControlEvents: UIControlEvents.TouchUpInside)
button.backgroundColor = UIColor.redColor()
addSubview(button)
}
func callOutButtonTouchAction(sender: AnyObject) {
println("callOutButtonTouchAction called")
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func hitTest(var point: CGPoint, withEvent event: UIEvent?) -> UIView? {
let viewPoint = superview?.convertPoint(point, toView: self) ?? point
let isInsideView = pointInside(viewPoint, withEvent: event)
var view = super.hitTest(viewPoint, withEvent: event)
return view
}
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
return CGRectContainsPoint(bounds, point)
}
}
每件事都按应有的方式工作,但我无法接收红色按钮的触摸动作。任何建议都将是可观的。