谷歌地图中的标记图像闪烁

时间:2016-02-23 09:34:02

标签: ios objective-c swift

我需要在谷歌地图中闪烁或动画标记。我尝试使用CALayer,但它只是闪烁一次。 " pulseAnimation.repeatCount = FLT_MAX"代码无效。它只闪烁一次。有什么方法可以解决我的问题吗?

   func loadMap(){
        let camera = GMSCameraPosition.cameraWithLatitude(-33.86,
            longitude: 151.20, zoom: 15)
        let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.myLocationEnabled = true

    // change map type
    mapView.mapType = kGMSTypeNormal

    self.view = mapView

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)

    //custom marker
    marker.appearAnimation = kGMSMarkerAnimationPop
    marker.icon = UIImage(named: "maprymarker")
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView
    let pulseAnimation = CABasicAnimation(keyPath: "opacity")
    pulseAnimation.duration = 1
    pulseAnimation.fromValue = 0
    pulseAnimation.toValue = 1
    pulseAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    pulseAnimation.autoreverses = true
    pulseAnimation.repeatCount = FLT_MAX
    marker.layer.addAnimation(pulseAnimation, forKey: nil)

}

1 个答案:

答案 0 :(得分:1)

添加动画委托方法,然后再次调用相同的函数。

func blink(){
    var i:Int = 0
    for marker in markerBlink{
        let pulseAnimation = CABasicAnimation(keyPath: "opacity")
        pulseAnimation.delegate = self
        pulseAnimation.duration = 1.0
        pulseAnimation.fromValue = 1
        pulseAnimation.toValue = 0
        pulseAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        pulseAnimation.autoreverses = true
        marker.layer.addAnimation(pulseAnimation, forKey: "\(++i)")
    }

}

override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
    if flag{

        blink()

    }
}