gpt4 book ai didi

ios - 正确子类化 MKOverlayRenderer

转载 作者:行者123 更新时间:2023-11-29 00:50:33 24 4
gpt4 key购买 nike

我试图在运行时修改 MKPolyline 的路径以避免它与另一个重叠。

我已经设法获得所有重叠点, 我想做的是在 func createPath() 中MKPolylineRenderer 的添加一个偏移量来做点,所以理论上,它应该用我添加的小偏移量绘制相同的路径,并且它不应该再重叠,但遗憾的是,这种情况没有发生,折线是以同样的方式绘制,就像没有改变一样。

我在 addPolyline() 之后第一次尝试这样做函数,但我读到,一旦你这样做,重绘多段线的一种方法是删除它并再次添加它,所以我决定,出于测试目的,在添加多段线之前完成所有这些,所以当我最终将其添加到 map ,它已经有了关于重叠点的信息,但这也没有用。

假设:

1. map 工作在不同的线程上,因此更改没有反射(reflect)出来,这与此有关。 这没关系。应该是这样来优化渲染的。

<强>2。完成此操作的正确方法不在 createPath() 中。函数。确实不是

  1. 我应该在 draw() 中应用 transform 渲染器的功能。 就是这样

这是createPath()函数

override func createPath()
{
let poly = polyline as! TransportPolyline

switch poly.id
{
case 1:
let newPath = CGMutablePath()

for index in 0...poly.pointCount
{
let point = poly.points()[index]
let predicate = { MKMapPointEqualToPoint($0, poly.points()[index]) }
//This is the offset I should apply
let offset: CGFloat = overlapsAtPoints.contains(predicate) ? 100000.0 : 0.0
//I tried to use a transform as well, but the result was the same
var transform = CGAffineTransform(translationX: offset, y: offset)

if index == 0
{
//Here I add the offset and/or the transform without success
newPath.moveTo(&transform, x: CGFloat(point.x) + offset, y: CGFloat(point.y) + offset)
}
else
{
//Here as well
newPath.addLineTo(&transform, x: CGFloat(point.x) + offset, y: CGFloat(point.y) + offset)
}
}

//Set the new path to the Renderer path property
self.path = newPath
default: break
}
}

<罢工> 这就是draw()函数

override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext)
{
let poly = polyline as! TransportPolyline

guard poly.id == 1 else {
super.draw(mapRect, zoomScale: zoomScale, in: context)
return
}

//If I apply this the Polyline does move, obviously it move all the Path and not only the segments I want.
context.translate(x: 1000, y: 1000)

super.draw(mapRect, zoomScale: zoomScale, in: context)
}

非常感谢任何建议。

更新:

我发现问题可能出在我如何在绘制方法中绘制上下文。

文档说:

The default implementation of this method does nothing. Subclasses are expected to override this method and use it to draw the overlay’s contents.

所以通过调用 super.draw()我什么都没做。

关于如何正确重写此方法有什么想法吗?还要考虑到这一点:

To improve drawing performance, the map view may divide your overlay into multiple tiles and render each one on a separate thread. Your implementation of this method must therefore be capable of safely running from multiple threads simultaneously. In addition, you should avoid drawing the entire contents of the overlay each time this method is called. Instead, always take the mapRect parameter into consideration and avoid drawing content outside that rectangle.

最佳答案

所以基本上我走在正确的轨道上,但使用了错误的工具。实现此目的的实际方法是重写 MKPolylineRenderer 子类中的 draw() 函数。

override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext)
{
//First validate that the Rect you are asked to draw in actually
has some content. See last quote above.
let theMapRect: MKMapRect = self.overlay.boundingMapRect;

guard (MKMapRectIntersectsRect(mapRect, theMapRect)) || self.path != nil else {
return
}

//Do some logic if needed.

//Create and draw your path
let path = CGMutablePath()
path.moveTo(nil, x: self.path.currentPoint.x, y: self.path.currentPoint.y)
path.addLines(nil, between: remainingPoints, count: remainingPoints.count)

context.addPath(path)

//Customise it
context.setStrokeColor(strokeColor!.cgColor)
context.setLineWidth((lineWidth + CGFloat(0.0)) / zoomScale)

//And apply it
context.strokePath()
}

通过这样做,我能够成功地为每个叠加绘制我想要的路径,没有任何麻烦。

关于ios - 正确子类化 MKOverlayRenderer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38169254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com