我的应用程序中有一个地图视图,它需要为每个地图注释定制calloutView。因此,我为这个定制的calloutView提供了一个XIB文件。这是我的地图视图控制器的代码
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
CustomCalloutView *calloutView = (CustomCalloutView *)[[[NSBundle mainBundle] loadNibNamed:@"CustomCalloutView" owner:self options:nil] objectAtIndex:0];
[calloutView.layer setCornerRadius:10];
CGRect calloutViewFrame = calloutView.frame;
calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
calloutView.frame = calloutViewFrame;
// some other code such as load images for calloutView
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap)];
singleTap.numberOfTapsRequired = 1;
[calloutView addGestureRecognizer:singleTap];
[view addSubview:calloutView];
}
- (void)handleSingleTap{
NSLog(@"it works");
}但是,handleSingleTap:从未被调用过。相反,calloutView上的每一个点击都只会简单地忽略calloutView。我还试图在calloutView上添加一个按钮,但是点击它也会导致calloutView被取消,而不是调用按钮操作。
有人能帮忙吗?
更新
我试着修改代码
[view addSubview:calloutView];至
[self.view addSubview:calloutView];它将自定义的calloutView添加到主容器视图中,而不是mapView。
然后,它可以很好的拍打手势。因此,我认为问题应该是由mapView引起的,似乎mapView将calloutView上的所有触摸事件都传递给了自己。有人对此有想法吗?
发布于 2014-06-18 02:43:52
好的。我终于想出了解决办法。我们需要为MKAnnotationView定制一个类来解决这个问题。以下是.m文件中的代码
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
UIView* hitView = [super hitTest:point withEvent:event];
if (hitView != nil)
{
[self.superview bringSubviewToFront:self];
}
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;
}希望这能对其他人有所帮助。
发布于 2014-06-12 14:33:54
您需要修改您的tap手势实现&方法called.Have查看并检查它是否工作。
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap)];
singleTap.numberOfTapsRequired = 1;
[calloutView addGestureRecognizer:singleTap];
[view addSubview:calloutView];
- (void)handleSingleTap:(UIGestureRecognizer *)recognizer {
NSLog(@"it works");
}https://stackoverflow.com/questions/24185424
复制相似问题