在 MKMapView 上顺序绘制和显示 MKMapAnnotations

Sequential plotting and display of MKMapAnnotations on MKMapView

我有一组要添加到 MKMapView 的地图注释。我希望能够一次添加一个,每次添加之间有一个短暂的停顿,这样我就可以显示过去旅程的进度。在查看 MKMapView 组件时,我看到了两种方法:

[self.mapView addAnnotation: ann];

[self.mapView addAnnotations: annArray];

使用数组同时将它们全部炸开,所以我尝试了一个循环过程,试图像这样呈现每个注释

    for (MapAnnotation *ann in _MapAnnotations) {
        //Add custom annotation to map
        [self.mapView addAnnotation: ann];

        //pause for 0.25 seconds
        usleep(250000);
    }

这也不起作用 - 进程暂停正常,但在绘制所有点之前不会完成渲染。我尝试使用 mapView setNeedsDisplay 语句强制渲染地图,但没有成功。有什么想法吗?

感谢 Duncan,我的代码现在看起来像这样,但是当我在填充过程中尝试滚动时我遇到了崩溃,有什么想法吗?

查看级别变量

@private BOOL userScrolling;

滚动按钮

//Next Day Clicked
- (IBAction)btnNext:(id)sender{
    //Move forward one day
    userScrolling = YES;
    [self setTheDate: 1];
}

绘制代码

    userScrolling = NO;


    [_MapAnnotations enumerateObjectsUsingBlock: ^(id object,
                                                   NSUInteger index,
                                                   BOOL *stop)
     {
         if (userScrolling){ *stop = YES;}

         dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
                                      (int64_t)( index * 0.25 * NSEC_PER_SEC)),
                        dispatch_get_main_queue(), ^
                        {
                            if (!userScrolling){
                                MapAnnotation *ann = _MapAnnotations[index];
                                [self.mapView addAnnotation: ann];
                                [self.mapView setNeedsDisplay];
                            }else{
                                *stop = YES;
                            }

                        }
                        );}];

永远,永远,永远在您应用的主线程上使用休眠。这会锁定 UI,并防止任何事情发生。如果您让主线程休眠超过几秒钟,系统将杀死您的应用,认为它已挂起。

相反,使用 dispatch_after 在调用之间添加延迟:

[_MapAnnotations enumerateObjectsUsingBlock: ^(id object, 
  NSUInteger index, 
  BOOL *stop)
  {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 
      (int64_t)( index * 0.25 * NSEC_PER_SEC)), 
      dispatch_get_main_queue(), ^
    {
      [self.mapView addAnnotation: ann];
    }
  }
];

该代码循环遍历您的注释数组,在从 0 开始并以 0.25 秒为间隔增加的延迟值之后添加每个注释。