gpt4 book ai didi

ios - MKOverlayView 和触摸

转载 作者:技术小花猫 更新时间:2023-10-29 11:19:09 28 4
gpt4 key购买 nike

我的 map 上有一个自定义 MKOverlayView,我想检测触摸。但是,我似乎无法让叠加层做出响应。我希望它会像忘记将 userInteractionEnabled 设置为 YES 一样愚蠢......但是,唉,运气不好

....目前,我是这样的:

//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;

}

我的自定义覆盖类简单地实现了 touchesBegan,并扩展了 MKCircleView

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch!");
}

最佳答案

首先,向您的 MKMapView 添加一个手势识别器(注意:这是假设 ARC):

[myMapView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mapTapped:)]];

在识别器操作中,您可以通过如下方式确定点击点是否在 View 中:

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

关于ios - MKOverlayView 和触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3472461/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com