我希望 MKMapView
继承自 UIScrollView
就像 UITableView
和 UICollectionView
一样。这样您就可以覆盖 UIScrollView
委托(delegate)方法并执行您需要执行的操作。
我找到了将平移手势附加到 MapView
的方法,如下所示:
UIPanGestureRecognizer* mapPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(mapPanGestureHandler:)];
[mapPanGestureRecognizer setDelegate:self];
[self.mapView addGestureRecognizer:mapPanGestureRecognizer];
除了 mapView
有一个减速元素外,这工作得很好。例如,如果您在中间滚动并抬起手指, map 会继续前进并减速然后最终停止。
我正在做的是使用核心图形在我的 map (另一个 UIView
)顶部的叠加层中呈现一些屏幕外注释。除了减速问题外,它工作得很好。
我只需要知道 map 已经移动。我需要的参数可以直接从 map 本身查询。
有没有人找到一种在减速时获得回调的技术?我的意思是我想我可以在手势结束时使用计时器重复触发半秒左右。
最佳答案
MKMapView
没有对 UIScrollView
的类似委托(delegate)回调,因此您需要即兴创作。
Has anyone found a technique to get callbacks while decelerating? I mean I suppose I could use a timer when the gesture ends to fire repeatedly for a half a second or so.
如果您想要准确地使 View 与 map 的移动保持同步,最好使用 CADisplayLink
,因为您会在每次屏幕刷新后收到回调。另一方面,如果您使用计时器,则回调不一定与刷新率保持一致,您可能会看到伪影。
你可以这样设置:
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateViewsBasedOnMapRegion:)];
[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
然后像这样处理回调:
- (void)updateViewsBasedOnMapRegion:(CADisplayLink *)link
{
// update whatever it is you need to update
}
您可以专门使用它并丢弃手势识别器或保留手势识别器并在不需要时暂停链接。
关于ios - MapView 检测滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23122173/