mapView:didDeselectAnnotationView:在注释视图实际取消选择之前调用委托方法

发布于 2024-11-27 15:49:25 字数 583 浏览 2 评论 0原文

我正在使用填充有自定义图钉的地图视图。当用户点击地图上的某处以取消选择图钉时,我想实现地图,以便该图钉不会被取消选择(即用户无法在不选择其他图钉的情况下取消选择图钉,因此至少有一个图钉将始终被选择)。这是我对 didDeselectAnnotationView 方法的实现:

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    [mapView selectAnnotation:view.annotation animated:NO];
}

本质上,我正在尝试重新选择注释。然而,经过一些调试并打印到控制台后,我意识到注释视图实际上并没有被取消选择,直到方法 didDeselectAnnotationView: 完成运行之后(也就是说,事件的顺序是:用户点击地图上的某处,调用 didDeselectAnnotationView:并完成执行,注释视图实际上被取消选择)。有没有其他人遇到过这个问题,或者是否有人知道另一种方法来强制地图的行为,使得用户无法在不选择其他图钉的情况下取消选择图钉,从而始终选择一个图钉?

感谢您的帮助。

I am working with a map view populated with custom pins. When the user taps somewhere on the map to deselect a pin, I want to implement the map such that the pin does not become deselected (i.e. users are not able to deselect pins without selecting other pins, so at least one pin will always be selected). Here is my implementation of the didDeselectAnnotationView method:

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    [mapView selectAnnotation:view.annotation animated:NO];
}

Essentially, I'm trying to reselect the annotation. However, after some debugging and printing to console, I realized that the annotation view isn't actually being deselected until after the method didDeselectAnnotationView: finishes running (that is, the order of events goes: user taps somewhere on map, didDeselectAnnotationView: is called and finishes executing, the annotation view is actually deselected). Has anyone else experienced this problem, or does anyone know another way to enforce the behavior for the map such that users are unable to deselect pins without selecting other pins, so that one pin will always be selected?

Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

归属感 2024-12-04 15:49:25

尝试将重新选择推迟到 didDeselectAnnotationView 完成之后:

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    [self performSelector:@selector(reSelectAnnotationIfNoneSelected:) 
            withObject:view.annotation afterDelay:0];
}

- (void)reSelectAnnotationIfNoneSelected:(id<MKAnnotation>)annotation
{
    if (mapView.selectedAnnotations.count == 0)
        [mapView selectAnnotation:annotation animated:NO];
}

Try deferring the re-select until after didDeselectAnnotationView is finished:

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    [self performSelector:@selector(reSelectAnnotationIfNoneSelected:) 
            withObject:view.annotation afterDelay:0];
}

- (void)reSelectAnnotationIfNoneSelected:(id<MKAnnotation>)annotation
{
    if (mapView.selectedAnnotations.count == 0)
        [mapView selectAnnotation:annotation animated:NO];
}
空城仅有旧梦在 2024-12-04 15:49:25

编辑:请记住,此方法会阻止选择任何其他引脚,而创建此答案时这是未知的。很可能,这不是您想要的行为。

我知道这是一个老问题,但接受的答案在 iOS 8 中对我不起作用。对我有用的是完全禁用默认情况下包含在 MKMapView 中的 UITapGestureRecognizer。

- (void)disableTapRecognizerForMapView:(MKMapView *)mapView {
    NSArray *a = [[self.mapView.subviews objectAtIndex:0] gestureRecognizers];

    for (id gesture in a)
        if ([gesture isKindOfClass:[UITapGestureRecognizer class]])
            [gesture setEnabled:NO];
}

希望这对其他人有帮助。

干杯!

EDIT: Keep in mind that this method prevents any other pins from being selected, which was unknown when this answer was created. Chances are, this is not your desired behaviour.

I know this is an old question, but the accepted answer didn't work for me in iOS 8. What worked for me was completely disabling the UITapGestureRecognizer that, by default, is included in a MKMapView.

- (void)disableTapRecognizerForMapView:(MKMapView *)mapView {
    NSArray *a = [[self.mapView.subviews objectAtIndex:0] gestureRecognizers];

    for (id gesture in a)
        if ([gesture isKindOfClass:[UITapGestureRecognizer class]])
            [gesture setEnabled:NO];
}

Hopefully this helps someone else.

Cheers!

殤城〤 2024-12-04 15:49:25

我也有类似的问题,但恰恰相反。

根据所选的引脚,表格将滚动到相应的单元格。如果未选择引脚,表格将滚动回第一个单元格。当选择另一个 pin 时,在调用 select 方法的同时调用取消选择方法,并且表格不会按要求滚动。

以下代码解决了该问题,并对 Anna 的解决方案进行了轻微修改。

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKPinAnnotationView *)view
{
   if ([view.annotation isKindOfClass:[MKUserLocation class]]) 
        return;

    [self performSelector:@selector(resetTableScroll:) 
           withObject:view.annotation afterDelay:.5];
    }



- (void)resetTableScroll:(id<MKAnnotation>)annotation{

    if (theMap.selectedAnnotations.count == 0)
    {
        NSIndexPath *position = [NSIndexPath indexPathForRow:0 inSection:0];
        [[self theTable] scrollToRowAtIndexPath:position atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }}

I had a similar issue, but the other way around.

Depending on the pin selected a table would scroll to the respective cell. If no pin was selected the table would scroll back to the first cell. The deselect method was being called at the same time the select method was being called when another pin was selected and the table would not scroll as required.

The following code resolved the issue and is a slight modification to Anna's solution.

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKPinAnnotationView *)view
{
   if ([view.annotation isKindOfClass:[MKUserLocation class]]) 
        return;

    [self performSelector:@selector(resetTableScroll:) 
           withObject:view.annotation afterDelay:.5];
    }



- (void)resetTableScroll:(id<MKAnnotation>)annotation{

    if (theMap.selectedAnnotations.count == 0)
    {
        NSIndexPath *position = [NSIndexPath indexPathForRow:0 inSection:0];
        [[self theTable] scrollToRowAtIndexPath:position atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }}
耶耶耶 2024-12-04 15:49:25

安娜的回答很棒。
这是 Swift 3 或 4 中的答案:D

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    perform(#selector(MyViewController.reSelectAnnotationIfNoneSelected(_:)), with: view.annotation, afterDelay: 0)
}

func reSelectAnnotationIfNoneSelected(_ annotation: MKAnnotation) {
    if mapView.selectedAnnotations.count == 0 {
        mapView.selectAnnotation(annotation, animated: false)
    }
}

Awesome answer from Anna.
Here the answer in Swift 3 or 4 :D

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    perform(#selector(MyViewController.reSelectAnnotationIfNoneSelected(_:)), with: view.annotation, afterDelay: 0)
}

func reSelectAnnotationIfNoneSelected(_ annotation: MKAnnotation) {
    if mapView.selectedAnnotations.count == 0 {
        mapView.selectAnnotation(annotation, animated: false)
    }
}
鸢与 2024-12-04 15:49:25

如果您为地图视图添加手势,请尝试以下操作:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

返回 NO,它可以工作

if you add a gesture for map view, try this:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

return NO, it works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文