mapView更改时如何刷新MKOverlayRenderer [英] How to refresh an MKOverlayRenderer when mapView change

查看:285
本文介绍了mapView更改时如何刷新MKOverlayRenderer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,我正在尝试实现类似Reminders app的东西:

I am new to code, and i'm trying to realize something like Reminders app :

我已经关注了另一个 answer 实现它和

I've follow another answer to realize it and

在我的ViewController中:

In my ViewController:

var circle = MKCircle(centerCoordinate: location.coordinate, radius: 100)
self.mapView.addOverlay(circle)

在我的MKMapView中:

In my MKMapView:

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {

    if overlay is MKCircle
    {
        render = MapFillRenderer(overlay: overlay)
        return render
    } else {
        return nil
    }
}

还有MapFillRenderer(MKOverlayRenderer的子类):

And the MapFillRenderer (subclass of MKOverlayRenderer):

class MapFillRenderer: MKOverlayRenderer {

    var colorIn: UIColor
    var colorOut: UIColor

    override func drawMapRect(mapRect: MKMapRect, zoomScale: MKZoomScale, inContext context: CGContext!) {
        // Fill full map rect with some color.
        var rect = self.rectForMapRect(mapRect)
        CGContextSaveGState(context);
        CGContextAddRect(context, rect);
        CGContextSetFillColorWithColor(context, colorOut.CGColor)
        CGContextFillRect(context, rect);
        CGContextRestoreGState(context);

        // Clip rounded hole.
        CGContextSaveGState(context);
        CGContextSetFillColorWithColor(context, colorIn.CGColor);
        CGContextSetBlendMode(context, kCGBlendModeClear);
        CGContextFillEllipseInRect(context, self.rectForMapRect(self.overlay.boundingMapRect))
        CGContextRestoreGState(context);

        // Draw circle
        super.drawMapRect(mapRect, zoomScale: zoomScale, inContext: context)
    }
}

问题:

但是当用户移动地图时,我遇到了一个问题,主蒙版没有刷新,并且它没有填充所有地图区域. 值得注意的是,它会刷新,但只有在我缩小得足够多的情况下才会刷新. 用户在不缩小地图的情况下移动地图时,如何强制刷新? 我尝试过,但是失败了:

The issue:

But I've an issue when the user move the map, the main mask doesn't refresh and it not fill all the map area. Something notable, it refresh, but only when i'm zoom out enough. How can I force it to refresh when user move the map without zoom out? I've try with, but it fail :

    func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
    if render.overlay != nil {
        render.setNeedsDisplay()
    }
}

感谢任何想法,

当用户不缩放就移动地图时,结果图像如下:

Here image of the result when user move the map without zoom:

推荐答案

MapKit通过图块调用渲染器.为了弄清楚要渲染的图块(在'MKMapRect中表达),它询问MKOverlay您的渲染器是否将在该图块上渲染.

MapKit calls your renderer by tile. To figure out which tiles (expressed in 'MKMapRect's) to render, it asks the MKOverlay if your renderer will render on that tile or not.

MKCircle的实现方式很可能只会对包含您的圆的图块说是".

MKCircle is very probably implemented in a way that will say yes only to those tiles that contain your circle.

因此,您需要覆盖var boundingMapRect: MKMapRect { get }以返回MKMapRectWorld或覆盖MKCircleoptional func intersectsMapRect(_ mapRect: MKMapRect) -> Bool.

So you need to override var boundingMapRect: MKMapRect { get } to return MKMapRectWorld or override optional func intersectsMapRect(_ mapRect: MKMapRect) -> Bool of MKCircle.

然后为显示给用户的每个图块调用渲染器.

Then your renderer is called for every tile that is shown to the user.

由于MKCircle主要用于计算圆上的rect并检查图块是否会与该rect相交,因此最好自己实现MKOverlay,只需返回MKMapRectWorld作为其boundingMapRec即可.

Since MKCircle is mostly about calculating the rect around the circle and checking if a tile will intersect with that rect, it is probably better to implement your own MKOverlay just returning MKMapRectWorld as its boundingMapRec.

这篇关于mapView更改时如何刷新MKOverlayRenderer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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