3

我正在尝试实现一个 MKCircle,它会随着滑块的变化而增加或减少半径。我遇到的问题是,当重新绘制圆圈时,它根本不平滑。我已经阅读了其他一些帖子,它们似乎表明您必须创建 MKCircle 的子类并以某种方式做到这一点,但是每当我查看示例代码时,我都很难理解,因为它通常不在 Swift 3 中。有人可以告诉我怎么做?这是我更改滑块时的代码:

func sliderValueChanged(_ sender:UISlider!) {
    if (!map.selectedAnnotations.isEmpty) {
        for overlay in map.overlays {
            var temp : MKAnnotation = (map.selectedAnnotations.first)!
            if (overlay.coordinate.latitude == temp.coordinate.latitude && overlay.coordinate.longitude == temp.coordinate.longitude) {
                let newCirc : MKCircle = MKCircle(center: temp.coordinate, radius: CLLocationDistance(Float(sender.value*1000)))
                let region: MKCoordinateRegion = MKCoordinateRegionForMapRect(newCirc.boundingMapRect)
                let r: MKCoordinateRegion = map.region
                if (region.span.latitudeDelta > r.span.latitudeDelta || region.span.longitudeDelta > r.span.longitudeDelta){
                    map.setRegion(region, animated: true)
                }
                map.add(MKCircle(center: temp.coordinate, radius: CLLocationDistance(Float(sender.value*1000))))
                map.remove(overlay)
                break
            }
        }

    }
}

到目前为止我所拥有的图片

4

2 回答 2

0

我发现在每个滑块值更改时更新圆圈大小并不顺利。我添加了一个检查,仅当滑块值更改超过 5 时才更新半径。这大大减少了所需的更新次数,从而大大提高了动画的平滑度。

var radius: Float = 0.0

@objc private func handleSliderMove() {
    guard let current = mapView.overlays.first else { return }

    let newRadius = CLLocationDistance(exactly: radiusSlider.value) ?? 0.0
    let currentRadius = CLLocationDistance(exactly: self.radius) ?? 0.0

    var diff = (newRadius - currentRadius)
    diff = diff > 0 ? diff : (diff * -1.0)

    if diff > 5 {
        self.mapView.addOverlay(MKCircle(center: current.coordinate, radius: newRadius))
        
        self.mapView.removeOverlay(current)
        self.radius = radiusSlider.value
    }
}
于 2020-07-01T01:15:03.720 回答
0

我解决了如下:

let currentLocPin = MKPointAnnotation()
var circle:MKCircle!

    func sliderValueDidChange(sender: UISlider) {

            map.remove(circle)

            circle = MKCircle(center: currentLocPin.coordinate, radius: CLLocationDistance(sender.value))
            map.add(circle)
    }

我希望这将有所帮助。

于 2017-06-12T22:52:53.490 回答