我有一个 map 注释数组,我想将其添加到 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语句来强制渲染 map ,但没有成功。有什么想法吗?
感谢邓肯,我的代码现在看起来像这样,但是当我尝试在填充过程中滚动时,我遇到了崩溃,有什么想法吗?
查看级别变量
@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;
}
}
);}];
最佳答案
永远,永远,永远在您应用的主线程上使用 sleep 。这会锁定 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 秒为间隔增加的延迟值之后添加每个注释。
关于ios - 在 MKMapView 上顺序绘制和显示 MKMapAnnotations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35564019/