当我调整NSSlider时,如何实现在UIMapView上MKCircleView的平滑缩放?苹果已经在“找朋友”应用程序中创建地理围栏(http://reviewznow.com/wp-content/uploads/2013/03/find-my-friends-location-alerts-01.jpg)时实现了这一点,所以我想这种方式是可行的。到目前为止,我尝试了以下解决方案,但结果非常“闪烁”:
在我的自定义覆盖视图中,我像这样实现了drawMapRect:zoomScale:inContext:(CGContextRef)context:
第一次尝试
我添加了一个新的MKCircleView,并更新了半径,然后立即删除了那个(如此建议 MKOverlay not resizing smoothly),当滑块改变值时。我还尝试了另一种方式:先删除覆盖层,然后再添加一个新的,但结果仍然很“闪烁”。
- (void)sliderChanged:(UISlider*)sender
{
double radius = (sender.value * 100);
[self addCircleWithRadius:radius];
[mapView removeOverlays:[self.mapView.overlays firstObject]];
}
第二次尝试
在链接的SO答案中,他建议可以使用NSOperation来“帮助您更快地创建MKCircle对象”,从而使调整大小更加平滑,使用上述方法添加/删除叠加层时,当幻灯片更改值时。我进行了一次实现,每当滑块更改时就启动一个新线程。在每个线程中,我会删除所有旧的叠加层,并添加一个新的叠加层以更新比例尺。也许他有其他种类的实现方式,因为我这样做时仍然会在更改滑块时出现闪烁。
- (void)sliderChanged:(UISlider*)sender
{
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(updateOverlayWithScale:)
object:[NSNumber numberWithFloat:sender.scale]];
[self.queue addOperation:operation];
}
每个线程中运行的方法:
- (void)updateOverlayWithScale:(NSNumber *)scale
{
MKCircle *circle = [MKCircle circleWithCenterCoordinate:self.currentMapPin.coordinate
radius:100*[scale floatValue]];
[self.mapView performSelectorOnMainThread:@selector(removeOverlays:) withObject:self.mapView.overlays waitUntilDone:NO];
[self.mapView performSelectorOnMainThread:@selector(addOverlay:) withObject:circle waitUntilDone:NO];
}
第三次尝试
我还尝试了实现自己的MKOverlayView子类,根据其比例属性绘制自身。每当滑块更改时,我调用setNeedsDisplay并让它重新绘制自己,但我仍然会看到闪烁。
- (void)sliderChanged:(UISlider*)sender
{
self.currentOverlayView.scale = sender.scale
[self.currentOverlayView setNeedsDisplay];
}
在我的自定义覆盖视图中,我像这样实现了drawMapRect:zoomScale:inContext:(CGContextRef)context:
- (void)drawMapRect:(MKMapRect)mapRect
zoomScale:(MKZoomScale)zoomScale
inContext:(CGContextRef)context
{
double radius = [(MKCircle *)[self overlay] radius];
radius *= self.scale;
// ... Create a rect using the updated radius and draw a circle inside it using CGContextAddEllipseInRect ...
}
那么,你有任何想法吗?提前感谢!