微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 平滑调整MKCircle的大小

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

第一次尝试

添加一个更新半径的新MKCircleView,并在滑块更改值后立即删除了(如此处建议的MKOverlay not resizing smoothly).我也尝试过另一种方法:首先删除叠加层,然后添加一个新叠加层,但使用相同的“flickery”结果.

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

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

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

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

}

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

解决方法

几个月前我偶然发现了 this animated MKCircleView.它还有一个关于git的演示.所以我想你应该试一试!
检查它,因为您可以通过滑块等调整它的需要.

积分转到yickhong提供此YHAnimatedCircleView

原文地址:https://www.jb51.cc/iOS/333205.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐