我想在iOS Swift中动摇谷歌地图标记 [英] I want to shake the google map marker in iOS Swift
问题描述
我正在研究一个项目,我想在Google地图上晃动标记。
我使用自定义标记图标在地图上表示。
就像一个人的头在颤抖。
我不知道该怎么做,并且搜索了很多,但是没有找到任何解决方案。
I am working on a project where I wants to shake the marker on google Map. I am using the custom marker icon to represent on the map. Like a head of person is shaking. I don't know how to do it and search a lot but didn't find any solution.
推荐答案
您可以将 CAKeyframeAnimation
或 CABasicAnimation
添加到 marker.iconView!.layer
我们添加一个比我们的UIImageView大的框架的UIView,然后我们需要将UIImageView的锚点调整到垂直和水平居中的底部,这将成为我们动画中的枢轴点,我们的动画将是Z平面上从-30到30级的旋转动画,以实现所需的动画。这是做到这一点的最简单方法,但您也可以定义一个自定义类并制作其他许多东西。
You can add a CAKeyframeAnimation
or CABasicAnimation
to your marker.iconView!.layer
we add a UIView with a frame bigger than our UIImageView inside then we need to adjust the anchor point of your UIImageView to bottom in vertical and horizontally centered this will be the point that will work as pivot in our animation, our animation will be a rotation animation in Z plane from -30 to 30 grades to achieve the animation desired.This is the simplest way to do this but you can also define a custom class and make a lot of other things
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
这里是它的样子
here is how it looks
希望这会有所帮助
这篇关于我想在iOS Swift中动摇谷歌地图标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!