首页 > 解决方案 > 在 Google 地图上使用动画在多个位置之间移动标记

问题描述

我想在两个位置坐标之间移动汽车图标,并且我想通过滑块更改该汽车图标的持续时间(速度)。

我尝试 CATransaction 为标记设置动画。它的动画效果很好,但我无法通过滑块实时更改持续时间(速度)。

这是我为标记设置动画的代码。

func moveTo(point: CLLocationCoordinate2D) {
        if let marker = arrMapList[0] as? GMSMarker {
         let angle = getAngle(fromLoaction: marker.position, toLocation: points[currentIndex])
        marker.rotation = CLLocationDegrees(angle * (180.0 / .pi))
            CATransaction.begin()
            CATransaction.setAnimationDuration(5)
            CATransaction.setCompletionBlock {
                if let nextPoint = self.getNextPoint(currentIndex: self.currentIndex) {
                    self.moveTo(point: nextPoint)
                }
            }
            marker.position = point
            CATransaction.commit()
    }
    }

我尝试如下更改持续时间,但它不起作用。

@IBAction func SliderValueChanged(_ sender: UISlider) {
    CATransaction.setAnimationDuration(CFTimeInterval(sender.value))
    }

标记(汽车)图标应该在两个坐标之间移动,如果用户通过左右移动滑块来改变速度值,动画持续时间应该会受到影响。

标签: iosswiftgoogle-maps

解决方案


您无法更改飞行中动画的持续时间。

您应该能够更改图层的速度参数以加快速度。

Speed = 1.0 是正常速度。速度 2.0 将快两倍(持续时间的一半)。0.5 的速度将是半速。

你会使用像这样的代码

marker.layer.speed = 2.0

(您需要重构以设置滑块的速度。尝试从 0.5 到 1.5 的范围。)


推荐阅读