平滑调整MKCircle的大小 [英] Smooth resizing of MKCircle

查看:215
本文介绍了平滑调整MKCircle的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调整NSSlider时,如何在UIMapView上平滑调整MKCircleView的大小?在创建地理围栏时,Apple已经设法在查找我的朋友应用程序中执行此操作( http://reviewznow.com/wp-content/uploads/2013/03/find-my-friends-location-alerts-01.jpg ),所以我想在某种程度上是可能的。到目前为止,我已经尝试了以下解决方案,但结果非常闪烁:

How can I achieve a smooth resizing of a MKCircleView on a UIMapView when adjusting an NSSlider? Apple has managed to do it in the Find my friends app when creating geofences (http://reviewznow.com/wp-content/uploads/2013/03/find-my-friends-location-alerts-01.jpg), so I guess it's possible in some way. So far I've tried the following solutions, but with a very "flickery" result:

首次尝试

我添加了一个更新半径的新MKCircleView,并在删除之后立即添加(如此处所示 MKOverlay不能平滑地调整大小)。我也尝试过另一种方法:首先删除叠加层,然后添加一个新叠加层,但使用相同的flickery结果。

I added a new MKCircleView with an updated radius and immediately after removing the one that was (as suggested here MKOverlay not resizing smoothly) when the slider changed value. I also tried the other way around: first removing the overlay then adding a new one, but with the same "flickery" result.

- (void)sliderChanged:(UISlider*)sender
{
    double radius = (sender.value * 100);
    [self addCircleWithRadius:radius];
    [mapView removeOverlays:[self.mapView.overlays firstObject]];
}

第二次尝试

在链接的SO答案中,他建议NSOperation可用于帮助您更快地创建MKCircle对象,从而在幻灯片更改时使用上述添加/删除叠加层的方法使调整大小更加平滑值。我做了一个实现,每当滑块改变时我就开始一个新的线程。在每个线程中,我删除所有旧的叠加层并添加一个带有更新比例的新叠加层。也许他还有其他一些实现方式,因为我这样做的方式在更改滑块时仍然会出现相同的闪烁。

In the linked SO answer, he suggests that NSOperation could be used to "help you create the MKCircle objects faster" and thus making the resizing smoother using the above method of adding/removing overlays when the slides changes value. I did a implementation where I start a new thread whenever the slider changes. In each thread I remove all old overlays and add a new one with the updated scale. Perhaps he has some other kind of implementation in mind, because the way I did it I still get the same flicker when changing the slider.

- (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子类,它根据scale属性绘制自己。每当滑块改变时,我调用setNeedsDisplay并让它自己重绘,但我得到相同的闪烁。

I also tried implementing my own subclass of MKOverlayView that draws itself based on its scale property. Whenever the slider changes I call setNeedsDisplay and let it redraw itself, but I get the same flicker.

- (void)sliderChanged:(UISlider*)sender
{
     self.currentOverlayView.scale = sender.scale
     [self.currentOverlayView setNeedsDisplay];
}

在我的自定义叠加视图中,我实现了drawMapRect:zoomScale:inContext:(CGContextRef )像这样的上下文

And in my custom overlay view I implement drawMapRect:zoomScale:inContext:(CGContextRef)context like this

- (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 ...

}

那么,你有什么想法吗?提前谢谢!

So, do you have any ideas? Thanks in advance!

推荐答案

几个月前我偶然发现了这个动画MKCircleView 。它还有一个关于git的演示。所以我想你应该试一试!
查看它,因为您可以使用滑块等调整它以满足您的需求。

A few months ago I stumbled upon this animated MKCircleView. It also has a demo on git. So I guess you should give it a go! Check it out as you might be able to tweak it to your needs with a slider etc.

积分转到yickhong提供此 YHAnimatedCircleView

Credits go to yickhong for providing this YHAnimatedCircleView

这篇关于平滑调整MKCircle的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆