我正在添加这样的标注 View :
func mapView(mapView: MKMapView!,
didSelectAnnotationView view: MKAnnotationView!) {
let calloutView = UIView(frame:
CGRect(x: 0, y: 0, width: 300, height: 120))
calloutView.backgroundColor = UIColor.purpleColor()
calloutView.center = CGPointMake(CGRectGetWidth(view.bounds) / 2.0, 0.0)
calloutView.layer.anchorPoint = CGPointMake(0.5, 1.0)
calloutView.layer.masksToBounds = false
calloutView.userInteractionEnabled = true
let calloutViewTapRecognizer = UITapGestureRecognizer(target: self,
action: "onCalloutViewTap")
calloutView.addGestureRecognizer(calloutViewTapRecognizer)
view.addSubview(calloutView)
}
虽然我的 onCalloutViewTap
函数从未被调用...我很想知道为什么,并想得到一些可以处理与我的 callout View 交互的东西。
最佳答案
这是因为您的注释 View 仅检测其边界内的触摸。由于您的标注 View 超出范围,因此 subview 无法识别水龙头。您需要覆盖注释 View 中的 pointInside:withEvent:
方法,这样您的标注才能真正检测到触摸。
这是一个 Objective-C 的例子:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
CGRect rect = self.bounds;
BOOL isInside = CGRectContainsPoint(rect, point);
if (!isInside)
{
for (UIView *view in self.subviews)
{
isInside = CGRectContainsPoint(view.frame, point);
if (isInside)
{
break;
}
}
}
return isInside;
}
编辑:
快速版本:
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
let rect = self.bounds
var isInside = CGRectContainsPoint(rect, point)
if (!isInside) {
for subview in subviews {
isInside = CGRectContainsPoint(subview.frame, point)
if (isInside) {
break
}
}
}
println(isInside)
return isInside;
}
关于ios - 如何处理自定义标注 View 上的点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28461487/