标注触摸事件

时间:2015-04-07 14:08:32

标签: ios objective-c

我有一个自定义的MKAnnotationView,我根据Apple Docs的文档自定义。

这是我的带有自定义标注视图的AnnotationView的代码。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    UIView *hitView = [super hitTest:point withEvent:event];
    if (hitView == nil && self.selected) {
        CGPoint pointInAnnotationView = [self.superview convertPoint:point toView:self];
        hitView = [self.calloutView hitTest:pointInAnnotationView withEvent:event];
    }
    return hitView;
}

- (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;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    [super setSelected:selected animated:animated];

    if (selected) {
        CGRect annotationViewBounds = self.bounds;
        CGRect calloutViewFrame = self.calloutView.frame;
        self.backgroundColor = [UIColor clearColor];

        // Center the callout view above and to the right of the annotation view.
        calloutViewFrame.origin.x = -(calloutViewFrame.size.width - annotationViewBounds.size.width) * 0.5;
        calloutViewFrame.origin.y = -calloutViewFrame.size.height + 1.0;
        self.calloutView.frame = calloutViewFrame;
        self.calloutView.layer.cornerRadius = 10;
        self.calloutView.layer.masksToBounds = YES;

        [self.calloutView setupCalloutView:self.info];

        [self addSubview:self.calloutView];
    } else {
        [self.calloutView removeFromSuperview];
    }
}

我在自定义标注视图中有一个按钮,我没有触摸操作。但我正在获得Custom Callout View的hitTest事件。

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
    UIView* hitView = [super hitTest:point withEvent:event];
    if (hitView != nil)
    {
        [self.superview bringSubviewToFront:self];
    }
    return hitView;
}

那么在自定义标注视图中获取触摸事件以打开详细视图控制器的正确方法是什么。

0 个答案:

没有答案