我正在尝试实现一个随着滑块的改变而增加或减少半径的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
}
}
}
}发布于 2017-06-13 06:52:53
我的解决方法如下:
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)
}我希望这能有所帮助。
发布于 2020-07-01 09:15:04
我发现在每次更改滑块值时更新圆的大小并不顺利。我添加了一个检查,仅当滑块值变化超过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
}
}https://stackoverflow.com/questions/44424109
复制相似问题