我想在iOS Swift中摇动谷歌地图标记

我想在iOS Swift中摇动谷歌地图标记,ios,swift,google-maps,swift3,google-maps-sdk-ios,Ios,Swift,Google Maps,Swift3,Google Maps Sdk Ios,我正在做一个项目,我想在谷歌地图上摇一摇标记。 我使用自定义标记图标在地图上表示。 就像一个人的头在颤抖。 我不知道怎么做,搜索了很多,但没有找到任何解决方案。您可以使用标记属性旋转标记 marker.rotation = (you_angle_in_degree) * M_PI / 180.0f; // convert degree to radian 在一段时间间隔后可以得到的水平抖动动画更改旋转的程度,可以使用计时器 // create a timer timer =

我正在做一个项目,我想在谷歌地图上摇一摇标记。 我使用自定义标记图标在地图上表示。 就像一个人的头在颤抖。
我不知道怎么做,搜索了很多,但没有找到任何解决方案。

您可以使用标记属性旋转标记

marker.rotation =  (you_angle_in_degree) * M_PI / 180.0f; 
 //  convert  degree to radian 
在一段时间间隔后可以得到的水平抖动动画更改旋转的程度,可以使用计时器

  // create a timer
  timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)

 func timerAction() {
       // Place you aniamtion logic here , 
       // every 0.5 second change the rotation of your maker
    }

您可以将
CAKeyframeAnimation
CABasicAnimation
添加到
标记.iconView!。层
我们添加了一个UIView,其中的帧大于UIImageView,然后我们需要将UIImageView的定位点调整为垂直和水平居中的底部这将是在动画中用作轴的点,我们的动画将是Z平面上的旋转动画,从-30级到30级,以达到所需的动画效果。这是实现这一点的最简单方法,但您也可以定义自定义类并制作许多其他内容

    let marker = GMSMarker(position: CLLocationCoordinate2D(latitude: 22.404963, longitude: -79.961755))

    //we need a bigger UIView to avoid the clip problem with the UIImageView
    marker.iconView = UIView(frame: CGRect(x: 0, y: 0, width: 60, height: 40))
    let imageView = UIImageView(frame: CGRect(x: (marker.iconView?.frame.width)!/2 - 14, y: (marker.iconView?.frame.height)! - 36, width: 28, height: 36))
    imageView.contentMode = .scaleAspectFit
    imageView.image = UIImage(named: "iconomapa")
    marker.iconView?.addSubview(imageView)
    imageView.layer.anchorPoint = CGPoint(x: 0.5, y: 1.0) //we need adjust anchor point to achieve the desired behavior
    imageView.layer.frame = CGRect(x: (marker.iconView?.frame.width)!/2 - 14, y: (marker.iconView?.frame.height)! - 36, width: 28, height: 36) //we need adjust the layer frame

    let animation = CAKeyframeAnimation()
    animation.keyPath = "transform.rotation.z"
    animation.values = [ 0, -30 * .pi / 180.0, 30 * .pi / 180.0 , 0]
    animation.keyTimes = [0, 0.33 , 0.66 , 1]
    animation.duration = 1;
    animation.isAdditive = false;
    animation.isRemovedOnCompletion = true
    animation.repeatCount = .infinity

    marker.iconView!.subviews[0].layer.add(animation, forKey: "shakeAnimation")
    marker.map = self.mapView
这是它的样子


希望这有帮助

您需要水平或垂直摇动的动画?您可以使用marker的旋转属性,您也可以这样做。我想水平摇动@ReinierMelianThanks@Reinier,但我在marker上得到零。iconView!。layer.add(动画,forKey:“shakeAnimation”)@ParthDhorda我的答案已更新,您需要用您的标记图像名称替换iconomapa。我希望插针应保持在同一条线上,但头部不应晃动整个插针Thanks@ParthDhorda你把大头针和头作为分开的图像吗?马克笔上只有一个大头针,想要这样摇晃