我是新来的代码,我试图实现类似的提醒应用:如何刷新MKOverlayRenderer当MapView的变化
我已经跟着另一个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()
}
}
感谢您的任何想法,
这里结果的图像时,用户移动地图,而缩放:
注意:提醒Apple应用程序,当您为位置添加提醒时,具有相同的错误 – grominet