How to handle taps on a custom callout view?

前端 未结 3 1781
Happy的楠姐
Happy的楠姐 2020-12-18 06:08

I am adding a callout view like this:

func mapView(mapView: MKMapView!,
        didSelectAnnotationView view: MKAnnotationView!) {
    let calloutView = UIVi         


        
相关标签:
3条回答
  • 2020-12-18 06:40

    Swift 4.0

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        let rect = self.bounds;
        var isInside: Bool = rect.contains(point);
        if(!isInside)
        {
            for view in self.subviews
            {
                isInside = view.frame.contains(point);
                if isInside
                {
                    break;
                }
            }
        }
        return isInside;
    }
     override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let hitView = super.hitTest(point, with: event)
        if (hitView != nil)
        {
            self.superview?.bringSubview(toFront: self)
        }
        return hitView
    }
    
    0 讨论(0)
  • 2020-12-18 06:41

    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;
    }
    
    0 讨论(0)
  • 2020-12-18 06:58

    Removing from annotation view swift 5

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
            let hitView = super.hitTest(point, with: event)
            if (hitView != nil)
            {
                self.superview?.bringSubviewToFront(self)
            }
            return hitView
        }
        override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
            let rect = self.bounds;
            var isInside: Bool = rect.contains(point);
            if(!isInside)
            {
                for view in self.subviews
                {
                    isInside = view.frame.contains(point);
                    if isInside
                    {
                        break;
                    }
                }
            }
            return isInside;
        }
    
    0 讨论(0)
提交回复
热议问题