我想在iOS中撼动Google地图标记Swift
我正在研究一个项目,我想要在Google地图上撼动标记。 我正在使用自定义标记图标在地图上表示。像一个人的头在摇晃。 我不知道如何去做和搜索很多,但没有找到任何解决方案。我想在iOS中撼动Google地图标记Swift
你可以添加一个CAKeyframeAnimation
或CABasicAnimation
到你的marker.iconView!.layer
我们添加一个比我们的UIImageView框架大的UIView,那么我们需要调整锚点你的UIImageView的点在垂直和水平居中的底部,这将成为我们动画中的枢轴点,我们的动画将是一个在-30到30级的Z平面上的旋转动画,以实现所需的动画。这是最简单的方式来做到这一点,但你也可以自定义一个类,并作出了很多其他的事情
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
这里是如何看起来
希望这有助于
谢谢@Reinier 但我得到无 marker.iconView!.layer.add(动画,forKey:“shakeAnimation”) –
@ParthDhorda我的答案已更新,您需要用您的替换iconomapa标记图像名称 –
我想引脚应该保持在相同的纬度和长度,但头部应该晃动不是整个针 谢谢 –
您可以使用标记属性来旋转你的标记,
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
}
你需要动画sh是水平的还是垂直的? –
你可以使用标记的旋转属性, – Dhiru
你也可以这样做 https://stackoverflow.com/questions/40543095/bounce-animation-on-google-map-marker-in-ios-objective-c – Dhiru