Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle taps on a custom callout view?

I am adding a callout view like this:

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)
}

Though my onCalloutViewTap function is never called... I am curious to understand why and to get something that works to handle interactions with my callout view.

like image 944
AsTeR Avatar asked Feb 11 '15 18:02

AsTeR


1 Answers

It's because your annotation view only detects touches inside its bounds. Since your callout view extends beyond the bounds, the subview doesn't recognize the tap. You need to override the pointInside:withEvent: method in the annotation view so your callout will actually detect the touch.

Here's an example in 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;
}

EDIT:

Swift version:

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;
}
like image 177
AdamPro13 Avatar answered Sep 22 '22 02:09

AdamPro13