我在我的地图上有一个自定义的MKOverlayView,我想要检测触摸。但是,我似乎无法让overlay响应。我希望它会像忘记将userInteractionEnabled设置为YES...but一样愚蠢。
....currently,这就是我是怎么做到的:
//map delegate overlay:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
if (_radiusView !=nil) {
[_radiusView removeFromSuperview];
[_radiusView release];
_radiusView = nil;
}
_radiusView = [[CustomRadiusView alloc]initWithCircle:overlay];
_radiusView.userInteractionEnabled = YES;
_radiusView.strokeColor = [UIColor blueColor];
_radiusView.fillColor = [UIColor grayColor];
_radiusView.lineWidth = 1.0;
_radiusView.alpha = 0;
//fade in radius view
[UIView beginAnimations:@"fadeInRadius" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.6];
_radiusView.alpha = .3;
[UIView commitAnimations];
return _radiusView;
} 我的自定义overlay类简单地实现了touchesBegan,并扩展了MKCircleView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch!");
}发布于 2012-06-26 15:57:29
首先,给你的MKMapView添加一个手势识别器(注意:这是假设的弧线):
[myMapView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mapTapped:)]];在识别器操作中,您可以通过类似以下内容来确定触点是否在视图中:
- (void)mapTapped:(UITapGestureRecognizer *)recognizer
{
MKMapView *mapView = (MKMapView *)recognizer.view;
id<MKOverlay> tappedOverlay = nil;
for (id<MKOverlay> overlay in mapView.overlays)
{
MKOverlayView *view = [mapView viewForOverlay:overlay];
if (view)
{
// Get view frame rect in the mapView's coordinate system
CGRect viewFrameInMapView = [view.superview convertRect:view.frame toView:mapView];
// Get touch point in the mapView's coordinate system
CGPoint point = [recognizer locationInView:mapView];
// Check if the touch is within the view bounds
if (CGRectContainsPoint(viewFrameInMapView, point))
{
tappedOverlay = overlay;
break;
}
}
}
NSLog(@"Tapped view: %@", [mapView viewForOverlay:tappedOverlay]);
}https://stackoverflow.com/questions/3472461
复制相似问题