如何在自定义标注视图上处理水龙头?[英] How to handle taps on a custom callout 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函数从未被调用...我很好奇地了解为什么并获得与我的标注视图一起处理交互的东西.

推荐答案

这是因为您的注释视图仅检测到其边界内的触摸.由于您的标注视图扩展到范围之外,因此该子视图无法识别点击.您需要在注释视图中覆盖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;
}

其他推荐答案

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
}

其他推荐答案

从注释视图中删除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;
    }

本文地址:https://www.itbaoku.cn/post/924367.html