首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何处理自定义标注视图上的点击?

如何处理自定义标注视图上的点击?
EN

Stack Overflow用户
提问于 2015-02-11 18:16:39
回答 3查看 1.5K关注 0票数 6

我正在添加这样一个标注视图:

代码语言:javascript
复制
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函数从未被调用..。我很好奇,想要了解为什么,并得到一些可以处理与我的标注视图交互的东西。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-02-11 18:50:12

这是因为您的注释视图只检测到其边界内的触点。因为您的标注视图超出了界限,所以子视图无法识别抽头。您需要覆盖注释视图中的pointInside:withEvent:方法,这样您的标注就可以实际检测到触摸。

以下是目标C中的一个例子:

代码语言:javascript
复制
- (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;
}

编辑

迅捷版:

代码语言:javascript
复制
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;
}
票数 10
EN

Stack Overflow用户

发布于 2017-12-02 11:22:11

Swift 4.0

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2019-06-08 12:24:24

从注释视图中移除快捷5

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28461487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档