为什么MKOverlayRenderer上的drawRect缓慢 [英] Why is drawRect on MKOverlayRenderer slow

查看:146
本文介绍了为什么MKOverlayRenderer上的drawRect缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个应用程序,我必须在MKMapView上放置一个MKCircle批注.但是,圆圈​​需要移动/调整大小.我知道有人问过这个问题,例如在MKMapView中移动MKCircle ,建议删除/添加注释以实现该行为.

For an app, I have to place a MKCircle annotation on a MKMapView. However, the circle needs to be moved/resized. I know this has been asked, e.g. Moving MKCircle in MKMapView where removing/adding the annotation is suggested to get that behavior.

但是,我认为绘图非常慢,就像这篇文章中的用户一样: MKCircle的平滑大小调整.由于现在有此问题的答案,因此该问题似乎使用了YHAnimatedCircleView.慢速是指,当我使用UISlider更改圆的半径时,重绘是草率的,并且我还可以注意到用于绘制它的图块.

However, I consider the drawing being very slow, like the user in this post: Smooth resizing of MKCircle. Since there are now answers to this question, the question seemed to use YHAnimatedCircleView. By slow I mean, when I use a UISlider to change the radius of the circle, the redrawing is sloppy and I can also notice the tiles being used to draw it.

我创建了一个小的测试代码,覆盖了MKOverlayRenderer子类的drawMapRect.代码如下:

I created a small test code, overriding drawMapRect of a subclasses MKOverlayRenderer. The code is as follows:

-(void) drawMapRect:(MKMapRect)mapRect
          zoomScale:(MKZoomScale)zoomScale
          inContext:(CGContextRef)context
{
    MKCircle* circleOverlay = (MKCircle*)self.overlay;

    CLLocationCoordinate2D point = circleOverlay.coordinate;          

    CGPoint locationPoint =
        [self pointForMapPoint:MKMapPointForCoordinate(point)];

    CGFloat radius =
        MKMapPointsPerMeterAtLatitude(circleOverlay.coordinate.latitude) *
        circleOverlay.radius;

    CGRect circleRect = CGRectMake(locationPoint.x - radius,
                                   locationPoint.y - radius,
                                   radius * 2.0, radius * 2.0);

    CGContextSaveGState(context);    

    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextFillEllipseInRect(context, circleRect);

    CGContextRestoreGState(context);
}

当我移动UISlider时,将删除旧注释,并放置具有不同半径的新MKCircle,并调用drawMapRect方法.

When I move the UISlider the old annotation is removed and the new MKCircle with different radius is placed and the drawMapRect method gets called.

但是,即使这个小例子在调整圆的大小时也显示草率的绘制.

However, even this small example shows sloppy drawing when resizing the circle.

问题:

  1. 上面的示例是否完全正确,或者我在绘制图形时做错了什么?
  2. 如果示例正确,为什么调整大小时重画圆圈太慢/草率?

推荐答案

在MKMapView上绘制覆盖图时,我遇到了类似的问题,我的问题是相对于地图移动了我绘制的圆.与圆的运动相比,这导致了地图的运动存在滞后的问题,好像圆在地图上滑动而不是固定在地图上一样.

I had a similar issue with drawing an overlay on an MKMapView, my issue was moving the circle I had drawn in relation to the map. This was causing a issue with there being lag in the movement of the map compared to the movement in the circle, looking as though the circle was gliding on top of the map as opposed to being fixed on the map.

我的解决方案是使用CADisplayLink来同步MKMapView的图形和我添加到其中的所有视图.我相信这将与您最初使用UISlider更改圆的半径的实现一起使用,但是如果没有该实现,我将无法对其进行彻底的测试.

My solution was to use a CADisplayLink in order to synchronise the drawing of the MKMapView and any views I had added to it. I believe this would work with your original implementation of changing the radius of the circle using a UISlider, however without the implementation, I can't thoroughly test this.

一个代码示例如下:

- (void)viewDidLoad {
    self.mapUpdateLink = [CADisplayLink displayLinkWithTarget:self
                                                 selector:@selector(updateMapView)];
    [self.mapUpdateLink addToRunLoop:[NSRunLoop mainRunLoop]
                             forMode:NSRunLoopCommonModes];
}

- (void)updateMapView {
    CGPoint centerPoint = [self.mapView convertCoordinate:self.circleView.centerCoordinate
                                            toPointToView:self.view];
    if (!isnan(centerPoint.x) && !isnan(centerPoint.y)) {
        self.circleView.center = centerPoint;
    }
}

我希望这可以帮助您解决问题.

I hope this helps you with your issue.

这篇关于为什么MKOverlayRenderer上的drawRect缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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