2015-05-30 37 views
2

我是新来的代码,我试图实现类似的提醒应用:如何刷新MKOverlayRenderer当MapView的变化

enter image description here

我已经跟着另一个answer意识到这一点,并

这里我的代码:

在我的ViewController:

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

在我的MKMapView:

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

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

而且MapFillRenderer(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) 
    } 
} 

问题:

但是我有一个问题,当用户移动地图时,主掩码不会刷新,并且不会填充所有地图区域。 值得注意的是,它刷新,但只有当我缩小足够。 如何在用户移动地图时强制刷新而不缩小? 我已经尝试,但它失败:

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

感谢您的任何想法,

这里结果的图像时,用户移动地图,而缩放:

enter image description here

+0

注意:提醒Apple应用程序,当您为位置添加提醒时,具有相同的错误 – grominet

回答

3

MapKit通过平铺调用您的渲染器。为了找出渲染的tile(用'MKMapRect'表示),它会询问MKOverlay渲染器是否在该tile上渲染。

MKCircle很可能以一种方式实现,只会对那些包含您的圈子的瓦片说yes。

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

然后,您的渲染器会针对显示给用户的每个图块调用。

由于MKCircle主要是关于计算绕了一圈矩形和检查,如果瓷砖将与矩形相交,它可能是更好地实现自己的MKOverlay刚刚返回MKMapRectWorldboundingMapRec