gpt4 book ai didi

iOS - 添加和删除 MKCircle 覆盖到 MapView 导致故障

转载 作者:行者123 更新时间:2023-11-30 11:02:42 26 4
gpt4 key购买 nike

我有一个 map View ,用户可以选择半径来显示区域。我添加一个 MKCircle 作为叠加层。当半径从 30 英里变为 1 英里时,当 MapView 放大时,MKCircle 的周边会出现明显的故障。该故障看起来有点像天赋。仅在放大而不是缩小时才会发生。\

由于缩放不断变化,我在添加另一个覆盖层之前删除了旧的覆盖层,但我认为这不是问题。

如何消除 MapView 缩放变化时圆圈上的故障?

@IBAction func newRadiusButtonTapped(sender: UIButton) {

// coordinate is the users location and span was 10 miles now it's 1 mile
let region = MKCoordinateRegionMake(location.coordinate, span)
mapView.setRegion(region, animated: true)

let circle = MKCircle(center: location.coordinate, radius: radius)

// remove old overlay before adding another one
for overlay in mapView.overlays {
mapView.remove(overlay)
}

view.layoutIfNeeded()
// mapView.layoutIfNeeded() I tried this but it didn't make a difference
mapView.add(circle)
}

30 miles

glitch

1 mile

最佳答案

我找不到导致故障的原因,但我找到了这个委托(delegate)方法 from this answer在mapView区域完成更改后收到通知的mapView上。我在那里添加了覆盖层

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) { 
}

简单的过程:

我创建了一个 MKCircle 类型的圆形属性?我还创建了 Bool 类型、名为 shouldAddCircle 的属性,并将其设置为 true。按下按钮时,我使用在按钮内创建的 MKCircle 初始化 Circle 属性,并将 shouldAddCircle 设置为 true。在按钮功能内,我删除了所有 map View 叠加层。

在委托(delegate)方法内,我现在检查 shouldAddCircle 属性是否为 true,如果是,则检查以确保 Circle 属性不为零。如果它们匹配,那么我将初始化的圆添加到 map View 中。将圆圈添加到mapView后,我必须将shouldAddCircle设置为false,因为每次用户滚动 map 时,regionDidChangeAnimated都会被调用,并且它会不断向 map 添加叠加层。

这是下面的代码。请务必在 viewDidLoad 中添加 mapView.delegate = self 并在所有内容之前设置 MKMapViewDelegate

var circle: MKCircle?
var shouldAddCircle = true

@IBAction func newRadiusButtonTapped(sender: UIButton) {

// coordinate is the users location and span was 10 miles now it's 1 mile
let region = MKCoordinateRegionMake(location.coordinate, span)
mapView.setRegion(region, animated: true)

let circle = MKCircle(center: location.coordinate, radius: radius)

// set the circle property to match the circle that was just created
self.circle = circle

// set this true
shouldAddCircle = true

// remove old overlay before adding another one
for overlay in mapView.overlays {
mapView.remove(overlay)
}
}

// this function gets called repeatedly as the mapView is zoomed and/or panned
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {

// make sure this is true because that means the user updated the radius
if shouldAddCircle {

// make sure the circle isn't ni
if let circle = self.circle {
// after the mapView finishes add the circle to it
mapView.add(circle)

// set this to false so that this doesn't called again until the user presses the button where they set it to true
shouldAddCircle = false
}
}
}

关于iOS - 添加和删除 MKCircle 覆盖到 MapView 导致故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53134293/

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